when I used `.freeze()`, is there a way to mark a ...
# kotlin-native
d
when I used
.freeze()
, is there a way to mark a method or lambda as non mutating, so i can call it from Swift without creating a
kotlin.native.concurrent.InvalidMutabilityException
?
o
why not just do
{ some code }.freeze()
?
d
are you saying i can pass that lambda to Swift and call it without throwing an
InvalidMutabilityException
?
o
Yes
d
not sure why i was getting the exception. i am freezing an instance of a class comprised of a String member and a lambda member
i *was freezing
i removed the
.freeze()
call, now things work
though i wonder if it could randomly crash one day...
i guess i don't get the invalid dereference because i am performing whatever "mutating" action on this thing from the right thread?
o
right, I missed the fact that exception is
InvalidMutabilityException
- it is thrown when attempting to mutate frozen objects
d
so my original question was... is there a way to mark a method as non-mutating
or a lambda
it seems like
.freeze()
is the safe thing to do, but then i can't call the lambda
o
You can call it, but if it mutates - exception will be thrown. And we do not provide mechanisms to deceive runtime.
d
i'm pretty sure i get that exception when i'm not mutating anything
but my idea of mutating and the runtime's idea of mutating could easily be different..
hmm i guess the lambda has access to its scope, and even though it doesn't modify anything, the runtime or compiler are not smart enough to know that?
actually the lambda may in fact modify something. but i think i was getting the exception without even calling the lambda..
s
Could you share your stack trace?
d
good point! it looks like a stack unrelated to the lambda in question is in fact attempting to mutate something that is in the lambda's subgraph.
i guess `.freeze()`ing that lambda is freezing basically my whole app state, rendering it inoperable even from Kotlin?
....freezing something for Swift, also makes it immutable to all of Kotlin, is that right?
s
Yes.
d
makes sense. thank you.