rkechols
01/06/2022, 7:54 PM/androidApp
portion and the /shared/src/androidMain
?
• Is it possible for an expect
declaration in the shared code (/shared/src/commonMain
) to find its actual
implementation in the /androidApp
portion?Nicolas Picon
01/06/2022, 8:20 PM/shared/src/androidMain
is the android-specific part of your shared module. This module will be provided as a dependency to your android application (/androidApp
).
You can see the shared module as a library or an SDK that your application relies on.Nicolas Picon
01/06/2022, 8:21 PMexpect / actual
are resolved at compile time and must be in the same module, so the answer to your question is no. However you can declare interface in your shared module that you implement in the applicationrkechols
01/06/2022, 8:24 PMactual
and which are put into the android application itself?Anton Afanasev
01/06/2022, 10:57 PMrkechols
01/06/2022, 11:01 PMAnders Oedlund
01/07/2022, 1:15 AMPaul Weber
01/07/2022, 1:19 PMSo is there a “rule of thumb” about what platform-specific things are put into the shared module usingand which are put into the android application itself?actual
expect / actual
is useful when you want to use platform specific functionality in your shared code.
So when you have some feature that you need on both platforms and it depends on some platform specific functionality, that’s a prime candidate for expect / actual
.
So for example if you want to build some really advanced caching logic for both platforms you’d probably want to share the code, therefore it wants to live in shared
module. The caching module needs a K/V store? Oh, we have that on both platforms (SharedPreferences & UserDefaults), let’s wrap that those few function calls in expect / actual
and we can put everything in shared
. Cool!
If you have something that you only do on android with no iOS counterpart or some feature where 90% of the code would live in the expect / actual
anyways, that’s probably a candidate for the androidApp
module.
I would not blow up the shared
module with functionality that you don’t share between platforms.Paul Weber
01/07/2022, 1:21 PMexpect / actual
, otherwise you don’t.Paul Weber
01/07/2022, 1:23 PMexpect / actual
vs interface
:
I’d always go with expect / actual
until there’s a good reason not to. It’s more explicit and you’re more “safe” since the compiler helps you out.Anton Afanasev
01/07/2022, 1:28 PMNicolas Picon
01/07/2022, 10:08 PMexpect / actual
is allow usage of native types instead of Kotlin types in your shared code.
For example:
Common
public expect class Timestamp
internal expect fun Timestamp.toInstant(): Instant
Android
public actual typealias Timestamp = Instant
internal actual fun Timestamp.toInstant(): Instant = this
iOS
public actual typealias Timestamp = NSDate
internal actual fun Timestamp.toInstant(): Instant = TODO()
The toInstant()
extension function allows shared code to use Instant
internally, while iOS is using NSDate
.
This makes usage of your library more native-like, which can be important when providing the library to 3rd parties