Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 330
mikera Avatar asked Jan 27 '11 14:01

mikera


1 Answers

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.

like image 55
cgrand Avatar answered Sep 29 '22 18:09

cgrand