Skip to content

convenience macros for messing with Clojure type definitions.

License

Notifications You must be signed in to change notification settings

janetacarr/match-set-and-get

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

match-set-and-get

Clojars Project

Convenience macros for messing with Clojure type definitions.

Usage

Currently there's only one macro here called with-setters. with-setters will create a protocol and reify it per mutable field in the type definition.

(with-setters
  (deftype Thinger [^:unsynchronized-mutable thing immutable-thing]))

(def t (->Thinger "hi" "there"))

(set-thing t "bye")

Motivation

I created with-setters because one can only call set! at the top-level of a type's method, not inside a lambda (they don't get closed over). This does not work:

;; From Clojuredocs.org
;; does not work.
(deftype Test [^:unsynchronized-mutable x]
    ITestProtocol
    (act [this] (fn [o] (set! x o))))

If you're trying to create a perforamnt game in CLJS, this is tricky since JS land loves to lean on 'callbacks'.

One way around this limitatiton is to create a setter method for the other methods to call. As you can imagine, it's annoying to try and create a separate protocol with setters for each mutable field in a type definition. I reckon this was done for a good reason, so use this macro if you understand that you actually need mutable fields across your type definition.

About

convenience macros for messing with Clojure type definitions.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages