Search code examples
data-structuresclojuretype-hintingdeftype

How to achieve a recursive deftype


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.


Solution

  • 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.