Clojure 静态类型

发布于 2024-10-02 03:17:19 字数 450 浏览 0 评论 0原文

我知道这对于 Lisp 爱好者(以及其他动态语言爱好者)来说可能听起来像是亵渎,但是增强 Clojure 编译器以支持静态(编译时)类型检查有多困难?

抛开支持和反对静态和动态类型的争论,这是否可能(而不是“这是可取的”)?

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

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

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

对可行性有什么想法吗?

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))

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

俏︾媚 2024-10-09 03:17:19

这肯定有可能。然而,我不认为 Clojure 会获得任何形式的弱静态类型——它的好处太少了。

然而,Rich Hickey 在多个场合表达了他对 Qi 语言强大、可选且富有表现力的打字功能的喜爱, http://www.lambdassociates.org/qilisp.htm

alt text

It certain possible. However I do not think that Clojure will ever get any form of weak static typing - it's benefits are too few.

Rich Hickey has however expressed on several occasions his like for the strong, optional, and expressive typing feature of the Qi language, http://www.lambdassociates.org/qilisp.htm

alt text

谈场末日恋爱 2024-10-09 03:17:19

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

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

素年丶 2024-10-09 03:17:19

是的!看起来有一个项目正在进行中,core.typed,以使可选的静态类型检查成为现实。请参阅 Github 项目 及其
文档

这项工作源自 Ambrose Bonnaire-Sergeant 的本科荣誉论文 (PDF),与 打字球拍系统。

Yes! It looks like there is a project underway, core.typed, to make optional static type checking a reality. See the Github project and its
documentation

This work grew out of an undergraduate honours dissertation (PDF) by Ambrose Bonnaire-Sergeant, and is related to the Typed Racket system.

谎言 2024-10-09 03:17:19

由于一次读取和评估一个表单,因此您无法进行前向引用,这使得这在一定程度上受到限制。

Since one form is read AND evaluated at a time you cannot have forward references making this somewhat limited.

兔小萌 2024-10-09 03:17:19

老问题,但有两点很重要:我认为 Clojure 不支持阅读器宏,只支持普通的 lisp 宏。现在我们有 core.typed 选项用于在 Clojure 中输入。

Old question but two important points: I don't think Clojure supports reader macros, only ordinary lisp macros. And now we have core.typed option for typing in Clojure.

猫弦 2024-10-09 03:17:19

声明可以有类型提示,因此可以声明一个“是”尚未定义但包含有关结构的数据的类型的 var,但这确实很笨拙,您必须在任何代码路径之前执行此操作可以在定义类型之前执行。基本上,您需要预先定义所有用户定义的类型,然后像平常一样使用它们。我认为这使得图书馆的写作有点黑客化。

我并不是想早点暗示这是不可能的,只是对于用户定义的类型来说,它比预定义的类型要复杂得多。这样做的好处与成本是应该认真考虑的。但我鼓励任何有兴趣的人尝试一下,看看是否可以成功!

declare can have type hints, so it is possible to declare a var that "is" the type which has not been defined yet but contains data about the structure, but this would be really clunky and you would have to do it before any code path that could be executed before the type is defined. Basically, you would want to define all of your user defined types up front and then use them like normal. I think that makes library writing somewhat hackish.

I didn't mean to suggest earlier that this isn't possible, just that for user defined types it is a lot more complicated than for pre-defined types. The benefit of doing this vs. the cost is something that should be seriously considered. But I encourage anyone who is interested to try it out and see if they can make it work!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文