Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mutually referring deftypes in Clojure

I want to implement transient and persistent! in my Clojure deftype. As far as I can tell, this means having another deftype, TransientMyThing, implement the necessary methods. Okay so far, but those two classes need to know about each other in order to return instances of each other.

I think I can do it by forward-declaring Clojure functions make-transient and make-persistent, then defining the deftype (by referring to that function), then implementing the functions with the now-existing types, but it seems pretty gross to me. Is there a better option?

Edit: that works, but it's still gross.

like image 366
amalloy Avatar asked Jun 19 '11 01:06

amalloy


1 Answers

In Clojure 1.3 and later, a slightly simpler solution is to rely on the constructor functions that Clojure creates for your deftypes, ->transient and ->persistent!. Since those are functions, and not macros, you can forward declare them. Then you can use them, rather than your own make-transient and make-persistent, and you don't have to implement them yourself.

like image 159
johnlamping Avatar answered Sep 28 '22 03:09

johnlamping