'Kotlin Multiplatform freeze object

I'm new to kotlin multiplatform, and I need to freeze an object as I'm still using the strict memory model. But I cannot solve this dependency on android studio, on my multiplatform module:

The following reference cannot be resolved: import kotlin.native.concurrent.freeze

I'm using kotlin 1.6.10 and multiplatform corutines version 1.6.0-native-mt.

Do I have to configure something else to be able to freeze objects on a multiplatform module?



Solution 1:[1]

As you can see from kotlin.native.concurrent.freeze package, and as you can check in documentation, it's only available on native - you can only use it from iosMain or other native platforms.

To use it from common code, you can use this simple library, or create expect/actual version of this function by yourself, and just return this on Android/JVM:

common:

expect fun <T> T.freeze(): T

Android/JVM:

actual fun <T> T.freeze(): T = this

iOS/Native:

import kotlin.native.concurrent.freeze

actual fun <T> T.freeze(): T = freeze()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1