Question

EDIT: After posting the previous version of my question I discovered that the real problem is with nested functions.

If I have a closure within a deftype I can not update any mutable fields from within that closure.

E.g. the following works:

(deftype Test [^:unsynchronized-mutable x]
    TestInterface
    (perform [this o] (set! x o)))

but this does not:

(deftype Test [^:unsynchronized-mutable x]
    TestInterface
    (perform [this o] (fn [] (set! x o)) nil)) ; throws a compiler error about assigning to non-mutable field

Is there any way to reach up and access the field? Doing (set! (.x this) o) results in:

ClassCastException user.Test cannot be cast to compile__stub.user.Test user.Test/fn--152 (NO_SOURCE_FILE:3

When trying to run the code.


Code for TestInterface for completeness:

(definterface TestInterface (perform [o]))
Was it helpful?

Solution

Consider that the version which does not work would, if it did work, return a closure which could escape into the wild and get called from wherever, setting your unsynchronized local and messing things up terribly.

If you insist on doing this sort of thing, you can always create an interface with a setter for your mutable field, implement it in your type and call the setter wherever you need to set this field.

Direct mutation ((set! (.-x foo) ...)) won't work, since mutable fields of Clojure types (both unsynchronized and volatile) are private.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top