Writing multiplatform Kotlin

In this previous section, we shared a single, simple class that exposed a platform-specific string. This demonstrated how we can leverage the expect and actual modifiers, but the overall example was quite simple. In this section, let's look at a slightly more interesting example of how we can architect common code and consume it across the different projects we've created.

For this, we perform the following steps:

  1. First, we're going to create a data class called ViewState to represent the data we want displayed to the screen. We'll define that class in a new file, core/src/commonMain/kotlin/ViewState.kt:
data class ViewState(val title: String, val subtitle: String)

ViewState will simply hold two strings: a title ...

Get Mastering Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.