Namespace is not loaded on defrecord class init

Description

As a user of Clojure interop from Java, I want defrecords (and deftypes?) to load their namespaces upon class initialization so that I can simply construct and use AOT'd record classes without manually requiring their namespaces first.

Calling the defrecord's constructor may or may not result in "Attempting to call unbound fn" exceptions, depending on what code has already been run.

This issue has been raised several times over the years, but I could not find an existing ticket for it:

Screened: Approach 2, which makes loading the namespace optional. This makes the change purely additive, whereas Approach 1 changes semantics (load ordering) of existing programs. I don't know why we would want that risk, especially when it is not clear that most users of deftype would even want ns loading side effect.

Approach 1: require the namespace a record/type belongs to during the record/type class init
Patch: 0001-CLJ-1208-load-own-namespace-in-deftype-defrecord-cla-v5-no-opts.patch

Approach 2: like Approach 1 but does the automatic loading only when a :load-ns option is set to true in the deftype/defrecord
Patch: 0001-CLJ-1208-load-own-namespace-in-deftype-defrecord-cla-v5.patch

Note: patch for Approach 1 causes some generative tests to fail since the namespace used to evalaute a defrecord is immediately destroyed thus impossible to load

Environment

None

Activity

Show:
Nicola Mometto
July 13, 2015, 2:47 PM

This is a bug in potemkin caused by the fact that it assumes deftype expands to an unqualified second argument.
The deftype* special form takes as first argument the tagsym and as a second argument the class name, potemkin passes a wrong second argument, it expands to :

rather than

I'll open a PR fixing this issue in potemkin and link back to this ticket.

Nicola Mometto
July 13, 2015, 7:15 PM
import
October 17, 2015, 10:27 AM

Comment made by: hellonico

Has this been applied ? Seems like it still break on 1.8beta1 ...

Caused by: java.lang.IllegalStateException: Attempting to call unbound fn: #'chapter01.core/shop-func
at clojure.lang.Var$Unbound.throwArity(Var.java:43)
at clojure.lang.AFn.invoke(AFn.java:32)
at chapter01.core.SuperMart.buy(core.clj:21)
at ShopSample.main(ShopSample.java:8)

Andy Fingerhut
October 17, 2015, 12:20 PM

This commit was made for on Jul 17 2015, so it is definitely included in 1.8.0-beta1: https://github.com/clojure/clojure/commit/c8e248d84740dbe1538c6f86ff1ecc59e200d23a

I haven't checked exactly which of the attached patches that corresponds to, or whether it fixes the problem.

Nicola Mometto
October 17, 2015, 12:21 PM

you'll have to set the :load-ns option to true in your defrecord declaration to make it work.
E.g

Assignee

Unassigned

Reporter

Tim McCormack

Approval

Ok

Patch

Code and Test

Fix versions

Priority

Major
Configure