clojure 2026-05-25

Quick question around primitive mutables in deftype:

(defprotocol P (set-b! [_ b]))
(deftype T [^:volatile-mutable ^boolean b]
  P (set-b! [_ b'] (set! b ^boolean b')))
This failed with an error:
Must assign primitive to primitive mutable: b
My guess is that ^boolean is assigning b' as the Boolean wrapper class, but is there any way around this?

(deftype T [^:volatile-mutable ^boolean b]
  P (set-b! [_ b'] (set! b (boolean b'))))

Ah, thanks!

AFAICT, set-b! being a protocol function prevents the b argument from being a primitive. So the ^boolean b' hint makes no sense - b' is never a primitive there. So you gotta coerce it.

☝️ 1

Right, I see. I guess I could always use definterface, but probably not worth the few nanoseconds I'd get from avoiding the coercion.

set-b! being a protocol function prevents the b argument from being a primitive.
This is not much about protocols, no Clojure functions support primitive arguments beyond longs and doubles.

Ah, of course. But it works in the exact same way with long anyway. :)

👍 1