Life · 4 / 4

An ID in shared code

An expect/actual exercise

Mediumkmpexpect-actual

In a module targeting Android and iOS, commonMain cannot import java.util.UUID. Practice the platform boundary by keeping Note in commonMain and implementing expect fun randomId(): String with Android and iOS actual declarations.

Contract: a lowercase UUID string in standard hyphenated form. Compute it once when a Note is created; reading the same Note again must not generate a new ID. Random generation does not promise mathematical impossibility of a collision.

Split the solution into commonMain/androidMain/iosMain files in the same package. Check both builds, string format and the stability of a single Note's ID. The editor represents several files using comments.

For an ordinary Kotlin 2.4.0+ project, common kotlin.uuid.Uuid.random() removes the need for a custom platform wrapper. Expect/actual remains an explicit learning constraint here. UUID stabilization.

Your code
Solution