Clojure静态类型 [英] Clojure static typing

查看:84
本文介绍了Clojure静态类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这听起来像是亵渎Lisp的狂热者(和其他动态语言的爱好者),但是如何提高Clojure编译器支持静态(编译时)类型检查有多困难?

I know that this may sound like blasphemy to Lisp aficionados (and other lovers of dynamic languages), but how difficult would it be to enhance the Clojure compiler to support static (compile-time) type checking?

设置静态和动态类型的参数,这是可能的(不是这是可取的)?

Setting aside the arguments for and against static and dynamic typing, is this possible (not "is this advisable")?

想到添加一个新的读取器宏来强制编译时类型(#^宏的增强版本),并将类型信息添加到符号表将允许编译器标记变量被误用的位置。例如,在下面的代码中,我会期望编译时错误(#*是编译时类型的宏):

I was thinking that adding a new reader macro to force a compile-time type (an enhanced version of the #^ macro) and adding the type information to the symbol table would allow the compiler to flag places where a variables was misused. For example, in the following code, I would expect a compile-time error (#* is the "compile-time" type macro):

(defn get-length [#*String s] (.length s))
(defn test-get-length [] (get-length 2.0))

#^宏甚至可以重用一个全局变量( * compile-time-type-checking * code>)强制编译器进行检查。

The #^ macro could even be reused with a global variable (*compile-time-type-checking*) to force the compiler the do the checks.

关于可行性的任何想法?

Any thoughts on the feasibility?

推荐答案

这当然是可能的。编译器已经在1.3开发分支中对基本参数类型进行了一些静态类型检查。

It's certainly possible. The compiler already does some static type checking around primitive argument types in the 1.3 development branch.

这篇关于Clojure静态类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆