Finally I started grasping/feeling when to use int...
# multiplatform
e
Finally I started grasping/feeling when to use interface/implementation and when expect/actual
🙌 4
🔥 3
f
Hey that’s cool. Is there any documentation you recommend? Or is it learning by trial and error?
p
This is my 2 grains. In general you can use interface/implementation always. But expect/actual is handy(you write less) when it can be used. It is all about how the real implementation is created in the different platforms. For instance, a global class, a class with no constructor arguments is better suited for expect/actual. Also if creating an instance of your class, depends on other classes that are global, or can be resolved without hooking into a specific platform lifecycle scoped dependency. Then you can expect/actual too. Now if your class depends on a dependency that only can be created using a specific platform API at a specific point through the lifecycle of an App. Then there is no way of doing expect/actual but you actually have to create your class instance at that specific moment and pass it down to your module.
👍 1
Eg: let's say you want a lifecycleManager class. This is a class that will keep a list of observers and will emit start/stop events when a screen enter/exit foreground/ background. There is no way to create a global class in each platform that would listen to these events. So can't expect/actual. In this case you have to wait for some platform specific class instance to hook your implementation to it. In iOS would be the willAppear/willDesapear callbacks in the viewController in Android the onStart/onStop in the Activity. So you actually have to wait for the platform to create your dependencies for you to create your implementation.
🙌 2
f
Thanks for the great explanation.
👍 1
e
For me, the key differentiation is the instantiation and order. Two examples. A Date/Time - I can pass a clock or calendar to instantiate time/date, but it is inconvenient and unnatural (except in the case of the unit tests, maybe). Here I would use except/actual. A biometric manager - here, I don’t need to create or manipulate it, I’m also okay that there is a place that will make it for me. Here I would go with interface/implementation. And disclaimer: I’m still a newbie with KMP/KMM, so this might/will change over time.
p
I believe your pattern fits my pattern just in different language. In your case Date/Time are global. Meaning you can construct/instantiate them without waiting for specific platform hook. They are not tied to the platform having to instantiate the dependency your class depends on for you. The BiometricManager is the case that is not global. Now you have to wait for the platform to give you this dependency at some point. Most likely as soon as the App starts eg: BiometricManager(appContext) but still, you depend on the platform instances life/scope to be able to create it.
This subject fits the classic "learning by example" concept, the more examples you do, the more you grasp it
👍 1
r
The other difference is that expect/actual is compile-time binding whereas interfaces are runtime binding. The former can be more performant in certain cases.
👍 2