core=> (defrecord Puffball [id name])
core.Puffball
core=> (Puffball. 149 "Urist")
#core.Puffball{:id 149, :name "Urist"}
core=> (apply Puffball. [149 "Urist"])
CompilerException java.lang.RuntimeException: java.lang.ClassNotFoundException: Puffball., compiling:(NO_SOURCE_PATH:3)
core=> (apply Puffball [149 "Urist"])
ClassCastException java.lang.Class cannot be cast to clojure.lang.IFn  clojure.core/apply (core.clj:600)


如何从向量创建泡泡球?

最佳答案

在Clojure 1.3中,记录定义会自动引入工厂功能:

Clojure 1.3.0
user=> (defrecord Puffball [id name])
user.Puffball
user=> (apply ->Puffball [149 "Urist"])
#user.Puffball{:id 149, :name "Urist"}


->Puffball是“位置”工厂功能;还有map->Puffball,它的名字暗示了这一点。

07-28 10:43