Stan Kurilin
Stan Kurilin

Reputation: 15792

Use record in several namespaces

I have clojure file with some predefined functions and records

;outer.clj
(ns outer )
(defn foo [a] (println a))
(defrecord M [id])

And now usage file

;inner.clj
(ns inner (:use outer ))
(foo 2)    ;works fine
(println (:id (M. 4))) ;throws IllegalArgumentException: Unable to resolve classname: M

Why does function imports fine but records definition doesn't? How I should import it?

Upvotes: 1

Views: 76

Answers (2)

Alex Taggart
Alex Taggart

Reputation: 7825

While sw1nn is correct, since 1.3 you needn't go through a separate import. Both defrecord and deftype also create constructor functions, which would be available via use/require just like any other function.

The function created by both follows the form ->MyType and takes positional args.

Additionally, defrecord creates a second constructor function that takes a map arg, map->MyRecord.

Upvotes: 4

sw1nn
sw1nn

Reputation: 7328

Because defrecord generates a JVM class 'under the covers' you need to import that class...

;inner.clj
(ns inner 
    (:use outer )
    (:import outer.M)
(foo 2)    ;works fine
(println (:id (M. 4))) ; works with import

Upvotes: 5

Related Questions