开发者

In Clojure how can I make my own deftype refable?

开发者 https://www.devze.com 2023-02-02 10:58 出处:网络
I wish to make a clojure deftype which implements Clojure Hashmaps. I realise that I can implement the Clojure Interfaces to make my deftype Hashable and Sequable, but what do I need to include to get

I wish to make a clojure deftype which implements Clojure Hashmaps. I realise that I can implement the Clojure Interfaces to make my deftype Hashable and Sequable, but what do I need to include to get my deftype to play nice with the Clojure STM so that I开发者_运维问答 can do:

(def a (ref (MyType.)))

and then to perform dosync operations such as conj and cons in an STM safe manner?


You problem is allready solved by records they are what you describe a type/class but with interfaces like Hashable and Sequable (and more).

You can just put your record in a ref like anyother hashmap. Don't see the problem here.


(def a (ref MyType)) doesn't make much sense because you are putting the class MyType in a ref.

The type that you put into a ref should ideally be an immutable type as the body of dosync should be free of side effects.

0

精彩评论

暂无评论...
验证码 换一张
取 消