How to achieve a recursive deftype
Asked Answered
M

1

11

I'm curious as to how to do a Clojure deftype that contains a reference to itself, e.g.

  (deftype BinaryTree [^BinaryTree left ^BinaryTree right])

This doesn't work... however I see no intrinsic reason why it shouldn't be possible since the underlying Java class is perfectly capable of referring to itself.

What am I doing wrong here?

Mike.

Monaco answered 27/1, 2011 at 14:17 Comment(0)
D
10

Currently ^Class hints on fields (in opposition to ^primitive hints) are discarded, so there's no gain in trying to put them. This may change in the future.

However auto reference in a type definition (eg in method bodies, not in fields) somewhat works but the implementation is a bit of a hack. There's little incentive to fix auto-reference in the current java compiler given the promise of the rewrite of the compiler in Clojure.

Demantoid answered 27/1, 2011 at 14:44 Comment(1)
thanks - very helpful explanation (though also annoying as I'd really like to use some recursive references!)Monaco

© 2022 - 2024 — McMap. All rights reserved.