Skip to content

Instantly share code, notes, and snippets.

@ryoakg
Forked from shinmuro/mutable-deftype.clj
Created April 10, 2016 01:19
Show Gist options
  • Save ryoakg/1f1ac1d93c2c9c8167816dee344c0dde to your computer and use it in GitHub Desktop.
Save ryoakg/1f1ac1d93c2c9c8167816dee344c0dde to your computer and use it in GitHub Desktop.
Clojure deftype that has mutable field and setter method sample.
(defprotocol IEditName
(get-name [this])
(set-name! [this val]))
(deftype PersonName [^:volatile-mutable lname]
IEditName
(get-name [this] (. this lname))
(set-name! [this val] (set! lname val)))
(def pname (PersonName. "hoge"))
;=> #'user/pname
(set-name! pname "fuge")
;=> "fuge"
(get-name pname)
;=> "fuge"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment