Inside the snapshot-lib project you will see these folders:
commonMain - compiles for all platforms
jvmMain - compiles only on the JVM
jsMain - compiles only to JS
commonTest - these tests run on all platforms
jvmTest - these tests only run on JVM
- etc.
Good example
We want as much code as possible to live in common. A good example of multiplatform is this:
- a simple
internal expect fun to wrap a platform-specific API
- all the logic of
ArrayMap lives in common
https://github.com/diffplug/spotless-snapshot/blob/f63192a84390901a3d3543066d095ea23bf81d21/snapshot-lib/src/commonMain/kotlin/com/diffplug/snapshot/ArrayMap.kt#L22-L115
Then we implement the "expect" for both jvm and js with "actual"
Work to be done.
In this case, we have JVM-only code. It should be pretty easy to move it into common, but there are a few tricky parts, which I don't want to deal with right now. So I move only the public API into common.
https://github.com/diffplug/spotless-snapshot/blob/f63192a84390901a3d3543066d095ea23bf81d21/snapshot-lib/src/commonMain/kotlin/com/diffplug/snapshot/PerCharacterEscaper.kt#L18-L21
Leave the implementation as jvm only, and the js just throws TODO exceptions for now
To implement this I would
- move
PerCharacterEscaperTest out of jvmTest and into commonTest
gradlew jvmTest will still be passing, but gradlew jsTest will now be failing
- remove the
expect actual stuff and just put the whole JVM implementation into common
- fix compile errors with as little platform-specific code as possible
Inside the
snapshot-libproject you will see these folders:commonMain- compiles for all platformsjvmMain- compiles only on the JVMjsMain- compiles only to JScommonTest- these tests run on all platformsjvmTest- these tests only run on JVMGood example
We want as much code as possible to live in
common. A good example of multiplatform is this:internal expect funto wrap a platform-specific APIArrayMaplives incommonhttps://github.com/diffplug/spotless-snapshot/blob/f63192a84390901a3d3543066d095ea23bf81d21/snapshot-lib/src/commonMain/kotlin/com/diffplug/snapshot/ArrayMap.kt#L22-L115
Then we implement the "expect" for both jvm and js with "actual"
Work to be done.
In this case, we have JVM-only code. It should be pretty easy to move it into common, but there are a few tricky parts, which I don't want to deal with right now. So I move only the public API into
common.https://github.com/diffplug/spotless-snapshot/blob/f63192a84390901a3d3543066d095ea23bf81d21/snapshot-lib/src/commonMain/kotlin/com/diffplug/snapshot/PerCharacterEscaper.kt#L18-L21
Leave the implementation as jvm only, and the js just throws TODO exceptions for now
To implement this I would
PerCharacterEscaperTestout ofjvmTestand intocommonTestgradlew jvmTestwill still be passing, butgradlew jsTestwill now be failingexpect actualstuff and just put the whole JVM implementation into common