curioustechizen
02/26/2020, 11:15 AMexpect/actual
. Is Kotlin code in commonMain
supposed to be able to access code in iosMain
or androidMain
even without using the expect/actual
mechanism?
So, if I have different implementations of fun getPlatformName(): String
each in iosMain
and androidMain
, but I don't declare expect fun getPlatformName(): String
in commonMain
am I still supposed to be able to call this function from commonMain
?Robert Jaros
02/26/2020, 11:19 AMcommon
code can only access other common
codeRobert Jaros
02/26/2020, 11:21 AMexpect
function or class in common
you can access it, even though the actual
code will be defined elsewhereRobert Jaros
02/26/2020, 11:22 AMcommon
curioustechizen
02/26/2020, 11:24 AMcommon
code to access anything else is if it is declared in common using the expect
keyword?curioustechizen
02/26/2020, 11:24 AMrusshwolf
02/26/2020, 1:31 PMexpect
though it's usually the quickest way to inject platform code into common. You can also define interfaces or lambda parameters in your common code and have your platform code implement them.curioustechizen
02/26/2020, 1:36 PMexpect
is not strictly needed. I kept coming up with situations where the interface approach does not work and my team-mate kept proving that I'm wrong.
So basically anything you can achieve with expect
you can also achieve with interfaces. Of course it is true that one might be more convenient than the other depending on the situationcurioustechizen
02/26/2020, 1:37 PMAlso, it requires common declarations to be expressed as interfaces, which doesn't cover all possible cases.I'm wondering what are the possible cases that are not covered by interfaces,
russhwolf
02/26/2020, 1:42 PMrusshwolf
02/26/2020, 1:42 PMrusshwolf
02/26/2020, 1:47 PMexpect
in my code. One of the limitations of expect
that I've run into is that you can't swap in other implementations very easily (eg for testing). So I almost never do expect class
and tend to limit it to top-level functions and values when I do use it.kpgalligan
02/26/2020, 3:15 PMkpgalligan
02/26/2020, 3:17 PMkpgalligan
02/26/2020, 3:19 PM