编辑: 在发布我的问题的先前版本后,我发现真正的问题在于嵌套函数。
如果我在 a 中有一个闭包,deftype
我将无法从该闭包中更新任何可变字段。
例如以下作品:
(deftype Test [^:unsynchronized-mutable x]
TestInterface
(perform [this o] (set! x o)))
但这不会:
(deftype Test [^:unsynchronized-mutable x]
TestInterface
(perform [this o] (fn [] (set! x o)) nil)) ; throws a compiler error about assigning to non-mutable field
有什么办法可以到达并进入该领域?这样做(set! (.x this) o)
会导致:
ClassCastException user.Test 无法转换为 compile__stub.user.Test user.Test/fn--152 (NO_SOURCE_FILE:3
尝试运行代码时。
完整性代码TestInterface
:
(definterface TestInterface (perform [o]))