Mutable fields in Clojure deftype?

后端 未结 2 1926
无人共我
无人共我 2021-01-31 19:01

I\'m trying out Clojure 1.2, specifically mutable fields which are supported in deftype according to the clojure.org documentation.

But I can\'t get the se

2条回答
  •  抹茶落季
    2021-01-31 19:46

    Like most things in Clojure, fields in types defined via deftype are immutable. While you can circumvent this using :volatile-mutable / :unsynchronized-mutable annotations, it is not common at all to do so. For one thing, such annotations will make the field private, so only the methods defined on the type will be able to access (and thus set) it. But more importantly, such constructs are susceptible to data races.

    When mutability is required, idomatic Clojure will use one of Clojure's reference types like atom or ref.

提交回复
热议问题