dynamic - Clojure 是单类型的吗?

标签 dynamic types clojure static-language

Robert Harper写了一篇引人入胜的作品,名为 "Dynamic languages are static languages" .他在其中写道:

And this is precisely what is wrong with dynamically typed languages: rather than affording the freedom to ignore types, they instead impose the bondage of restricting attention to a single type! Every single value has to be a value of that type, you have no choice!



has come to mean uni-typed languages . (单一类型的语言)。

现在 Clojure claims to be a dynamic language :

Clojure is a dynamic programming language that targets the Java Virtual Machine (and the CLR, and JavaScript). It is designed to be a general-purpose language, combining the approachability and interactive development of a scripting language with an efficient and robust infrastructure for multithreaded programming. Clojure is a compiled language - it compiles directly to JVM bytecode, yet remains completely dynamic. Every feature supported by Clojure is supported at runtime.



“动态”的意思是“可以在运行时进行交互”,而不是“没有类型”。

现在,静态和动态之间的关键区别似乎是“我可以在编译时遇到类型错误吗?”

如果我编写以下 Clojure 代码:
(deftype circle-type [radius] )

(deftype square-type [side-length])


(defn def-check-type [new-symbol-type existing-symbol]
 (let [existing-symbol-type (type existing-symbol)]
    (cond 
     (= new-symbol-type existing-symbol-type) 
     (def new-symbol existing-symbol)
     :else (str "types didn't match: " new-symbol-type " : " existing-symbol-type))))

(println (def-check-type circle-type (square-type. 2)));)

然后在莱宁根编译:
lein uberjar

然后我得到以下信息:
$ lein uberjar
Compiling clojure-unittyped.core
types didn't match: class clojure_unittyped.core.circle-type : class clojure_unittyped.core.square-type

这似乎是动态语言在编译时的类型失败。

我的问题是: Clojure 是单类型的吗?

编辑 - 我知道 core.typed ——这是一部优秀的作品。我在问这个问题。

最佳答案

是的,Clojure 是单类型的,但是 Sam Tobin-Hochstadt argues单类型分类在实践中的信息量并不大。

单类型理论几乎没有揭示动态类型语言编程的本质。它主要用于证明类型理论中“动态”类型的存在。

项目如 Typed Racket正是为了发现和建模程序员在此类语言中使用的隐式类型信息而发明的。通常类型是非常准确的,并且表明发生的事情比眼睛看到的要多得多。

关于dynamic - Clojure 是单类型的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23282683/

相关文章:

c# - 问号在 MVC 中意味着什么?

typescript - 具有未知键的属性的 Io-ts 接口(interface)

parsing - 如何将 instaparse 输出转换为可以评估的函数?

macros - 让表格: How to access destructured symbols in a macro?

clojure - 其他 Clojure 项目源文件

javascript - 为什么我不能向我的 HTML 表格中动态添加行?

javascript - 有没有办法打印动态变量?

javascript - Javascript 中性别的动态对象值

javascript - 如何使用 jquery 将新的 <td> 附加到第 n 个 <tr> ?

haskell - 将类型参数限制为 Monoid