Hey Everyone, I’m in the early stages of exploring...
# multiplatform
v
Hey Everyone, I’m in the early stages of exploring Multiplatform in personal project. I wrote an  expect/actual function to retrieve user input and it seemed neat. Then I wrote another function and refactored a few things and the expect/actuals broke. I still don’t quite know why, but in digging around looking for more information about expect/actual I realized that maybe interfaces would be a better way to go. So the question is: since we already have interfaces, what is the actual benefit of using expect/actual, or what is a situation where it is better or even necessary when compared to using an interface?
b
Interfaces are generally preferred and recommended. expect/actual is mainly for library authors that needs to bring down public API to common layer and provide actual implementations under the same API for each platform. expect/actual plays nlicely with interfaces. Consider this example:
Copy code
// commonMain
interface Environment {
  val HOME: String
  val SHELL: String
}

expect fun getEnv(): Environment

// jvmMain
actual fun getEnv(): Environment = object: Environment {
  override val HOME = System.getenv("HOME")
  override val SHELL = System.getenv("SHELL")
}

// linuxX64Main
actual fun getEnv(): Environment = object: Environment {
  override val HOME = platform.posix.getenv("HOME")
  override val SHELL = platform.posix.getenv("SHELL")
}
You need some touchpoint to provide instances of your interfaces someplace in your API and expect/actual allows you to do just that. Aditionally, expect/actuals is handy when you want to ensure that implementations for things exist in all platforms as opposed to interfaces where implementations are optional.
v
Okk that's great
b
You can even use
expect interface
where you have api overlaps between platforms, but want to provide platform-specific enchancements on it as well. Those enchancements will not be visible in common code.