Expect and Actual Mechanism(Kotlin Multiplatform Learning Native UI Tutorial)

preview_player
Показать описание

--
Hello, RingRing here.
Welcome back to the Kotlin Multiplatform Learning Series for Android Engineers.
In this video, we'll dive into the Expect/Actual mechanism of the Native UI version's official tutorial.
Expect/Actual serves as a mechanism to implement platform-specific logic using Kotlin Multiplatform.
Expect and actual play roles similar to interface and override in platform-specific versions.
When you use expect on a common module, you declare a function as if it were an interface, specifying that the implementation will be written for each platform.
For each function marked with expect, you implement the actual functionality on each platform by using the actual keyword.
In essence, actual behaves like an override for expect on each platform.
Let's take a look at some real code.
In this example, we've defined a function called getPlatform, marked with expect, which returns a Platform.
For Android and iOS, we have actual implementations of getPlatform.
Calling getPlatform on Android returns a string representing the SDK version, while on iOS, it returns the device name and version.
The expect and actual mechanism ties back to the shared module's source set concept we discussed in the previous video.
You place the expect functions in the commonMain source set of the shared module.
The actual implementation on Android goes into the androidMain source set, and on iOS, it goes into the iosMain source set.
To summarize, expect and actual form a mechanism in the commonMain source set of the shared module to modify behavior for each platform, akin to the interface and override concept.
Thank you for watching.
If you found this guide helpful, don't forget to hit the "like" button, subscribe, and click the bell icon.
Stay tuned for more content on Kotlin Multiplatform.
Until next time, happy coding.
Рекомендации по теме
welcome to shbcf.ru