是否可以为deftype定义构造函数?

时间:2012-09-11 01:53:22

标签: clojure

deftype

的构造函数的一个简单案例
(deftype Atom [v]
    ...)

我想要将v包裹在原子中以便

@(.v (Atom. 1)) => 1

这可能吗?

2 个答案:

答案 0 :(得分:3)

不。如果你真的想要使用构造函数的OO方式,那么你需要使用gen-class

在功能世界中,使用函数非常简单。

(defn createAtom [v] (Atom. (atom v)))
@(.v (createAtom 1)) => 1

答案 1 :(得分:0)

以前曾问过同样的问题,请看:Add constructor to deftype created class

相关问题