Function 在多个名称空间中使用记录
我有clojure文件和一些预定义的函数和记录Function 在多个名称空间中使用记录,function,clojure,namespaces,record,Function,Clojure,Namespaces,Record,我有clojure文件和一些预定义的函数和记录 ;outer.clj (ns outer ) (defn foo [a] (println a)) (defrecord M [id]) 现在是用法文件 ;inner.clj (ns inner (:use outer )) (foo 2) ;works fine (println (:id (M. 4))) ;throws IllegalArgumentException: Unable to resolve classname: M
;outer.clj
(ns outer )
(defn foo [a] (println a))
(defrecord M [id])
现在是用法文件
;inner.clj
(ns inner (:use outer ))
(foo 2) ;works fine
(println (:id (M. 4))) ;throws IllegalArgumentException: Unable to resolve classname: M
为什么函数导入很好,但记录定义不好?我应该如何导入它?因为defrecord生成了一个“隐藏”的JVM类,所以您需要导入该类
;inner.clj
(ns inner
(:use outer )
(:import outer.M)
(foo 2) ;works fine
(println (:id (M. 4))) ; works with import
因为defrecord生成了一个“隐藏”的JVM类,所以您需要导入该类
;inner.clj
(ns inner
(:use outer )
(:import outer.M)
(foo 2) ;works fine
(println (:id (M. 4))) ; works with import
虽然sw1nn是正确的,但由于1.3,您不需要进行单独的导入。
defrecord
和deftype
都可以创建构造函数,与任何其他函数一样,可以通过use
/require
使用构造函数
两者创建的函数都遵循形式->MyType
,并采用位置参数
另外,
defrecord
创建第二个构造函数,该构造函数在sw1nn正确时接受map arg,map->MyRecord,因为1.3不需要单独导入。defrecord
和deftype
都可以创建构造函数,与任何其他函数一样,可以通过use
/require
使用构造函数
两者创建的函数都遵循形式->MyType
,并采用位置参数
此外,defrecord
还创建了第二个构造函数,该构造函数接受map arg,map->MyRecord
感谢您提供更多信息。感谢您提供更多信息。