Failed to import record names

Reading the above made me realise I’m not really clear on what a namespace alias is. If C is any java class in namespace test (eg. created via a defrecord), and we alias test in another namespace to t, evaluating C with or without the alias produces different results, eg.

(type test.C)
; java.lang.Class
(type t/C) 
; Syntax error compiling at ..
; No such var: t/R

I guess I had taken ‘alias’ literally to mean that evaluating an alias-qualified symbol (not sure what else to call it) was exactly equivalent to evaluating the corresponding fully-qualified one.

In reality the latter retrieves any value from the namespace map (clojure var or java class alike). The former seems only to retrieve vars.

I realise @seancorfield I may only be rewording what you write above (referring to ‘Clojure qualified syntax’), but it’s helpful for me to make explicit my earlier false assumption.

Java classes do not live in namespaces. They live in packages. That’s why t/C doesn’t work.

defrecord creates a Java class in a package name that is similar to the namespace name (- in the namespace will be replaced by _ for example). But the package name and the namespace name are in separate spaces.

You can alias a namespace and then use the alias to refer to Clojure names (but not Java class names).

There is no way to alias a Java package name. You can either use the fully-qualified (package + class) name or you can use import to create a shortcut so that you can use just the class name on its own.

But there is a symbol for the class in the namespace map, eg in the above case

((ns-map 'test) 'C)

evals to test.C

Hence my puzzlement over the difference between using the alias vs fully-qualified lookups. I had assumed (falsely) that both just looked up the value (clojure var or java class) for the key (symbol).

That’s more a convenience of implementation: when you are inside your test namespace, you can refer to C without a qualifier (without a package name). ns-map produces a combination of the public symbols and imports. ns-imports will give you the Java class names. ns-publics will give you the (public) Clojure names. Again, they are separate spaces.

You’ll see if you (import '(java.util Date)) there will now be an entry in the ns-map result for Date – but I hope you wouldn’t expect to be able to reference test/Date or test.Date from another namespace?

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.