Search code examples
kotlinkotlin-multiplatform

data classes in Kotlin multiplatform programming


I`m new at kotlin and want to build a multiplatform application. For the common part I want to use data classes that contains platform specific functions.

Is it possible to use the kotlin data class in a platform specific declaration?

something like

expect data class Foo(val bar: String)

best regards


Solution

  • From Kotlin's docs on Platform Specific Declarations:

    Expected declarations never contain any implementation code.

    Since data classes generate implementations they can't be used in expect declarations. The actual implementation can be a data class since it does not change the semantics of the declared class.