Setting a mutable field in a nested function - deftype - clojure

核能气质少年 提交于 2019-12-11 01:48:51

问题


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]))

回答1:


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.



来源:https://stackoverflow.com/questions/18236241/setting-a-mutable-field-in-a-nested-function-deftype-clojure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!