hi all, does someone knows why this happens and ho...
# korge
p
hi all, does someone knows why this happens and how can it be fixed? kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen kotlin.collections.HashMap@2fbf0c8 at kfun:kotlin.Throwable.<init>(kotlin.String?;kotlin.Throwable?)kotlin.Throwable (00000000007e0e00) at ThrowInvalidMutabilityException (00000000004c8d50) at kfun:kotlin.collections.HashMap.<set-length>#internal (00000000007ea080) at kfun:kotlin.collections.HashMap.addKey$stdlib(K)kotlin.Int (00000000007eaea0) at kfun:kotlin.collections.HashMap.put(K;V)V? (00000000007ec770) at kfun:com.senaptec.toolframework.util.FontLoader.$loadCOROUTINE$108.invokeSuspend(kotlin.Result<kotlin.Any?>)kotlin.Any? (0000000000560d10)
is it because it's an object?
message has been deleted
n
Is this on Mac target ?
p
mac and mingw
c
Most likely, yes. Objects and all other top-level constructs are frozen on creation, as are all its properties. Even though its a MutableMap, that map is still frozen and can’t be edited
p
s*it
ok, what about create a singleton with companion ?
should that work fine ?
c
Nope, companion objects are still considered top-level. Super annoying, I know. You will have to use one of the atomics from the Stately lib to do any modification on singletons https://github.com/touchlab/Stately
p
🤦‍♂️
i just want to have a global var to store references to fonts
or to whatever
c
Yeah I know, it drives me crazy. The alternative is to make it a local object and use DI to it where you need it. Korge has an injector module which could probably make this a bit less painful
p
uhmm, good idea
c
p
what about korge 2 with its new kotlin mp version?
solved in there?
c
I haven’t tried 2.0 yet. But it’s not a limititation of korge, it’s a fundamental restriction from Kotlin Native. No way around it other than using atomics or not using global objects
p
ok, will go with DI, in fact i like the idea of using DI in the project from an architecture point of view
is there any doc apart from this page regardin korinject? https://korlibs.soywiz.com/korinject/
c
This video tutorial touches briefly on it, but I’m not aware of any other docs

https://youtu.be/pdUEQP1Ry08?t=360

p
thx man, watching now
works like a charm with mapSingleton
👍 thx man
🙇‍♂️ 1