Nested types in clojure?
Asked Answered
W

2

7

In clojure, how do I type type hint a type that I have created? (I want to nest the types.)

e.g. I had thought that this would work:

(deftype A 
    [#^somePrimitive  someField])

(deftype B
    [#^A Avalue])

This brings up an error message:

Unknown location:
  error: java.lang.ClassNotFoundException: A

Note: clojure types are a new feature which currently exists only in the 'new' branch of clojure.

Edit: I was confused by the different ways of type hinting in clojure for java and clojure types. java classes are hinted as

#^java.some.class

while clojure types are hinted as:

#^:some.Namespace/type
Withdrawn answered 29/12, 2009 at 19:27 Comment(0)
L
11

For each deftype, a type tag (basically a namespace-qualified keyword) is created so you don't have to AOT-compile your code before you can use the resulting class.

If type A is in the current namespace, you can write it like this:

(deftype B [^::A Avalue])

For types in other namespaces, qualify the keyword using its namespace:

(deftype B [^:user/A Avalue])
Lapointe answered 30/12, 2009 at 19:53 Comment(0)
R
3

(deftype B [#^:user/A Avalue]) works for me.

Rameriz answered 30/12, 2009 at 0:12 Comment(2)
That seems to work, but then how would I do the same thing in other files? +1, anyhow.Withdrawn
Yup, I figured it out now. Thanks alot.Withdrawn

© 2022 - 2024 — McMap. All rights reserved.