|
1 | 1 | [//]: # (title: Expected and actual declarations)
|
2 | 2 |
|
3 |
| -Expected and actual declarations allow you to access platform-specific APIs from Kotlin Multiplatform modules. |
4 |
| -You can provide platform-agnostic APIs in the common code. |
5 |
| - |
6 |
| -> This article describes the language mechanism of expected and actual declarations. For general recommendations on |
7 |
| -> different ways to use platform-specific APIs, see [Use platform-specific APIs](https://www.jetbrains.com/help/kotlin-multiplatform-dev/multiplatform-connect-to-apis.html). |
8 |
| -> |
9 |
| -{type="tip"} |
10 |
| - |
11 |
| -## Rules for expected and actual declarations |
12 |
| - |
13 |
| -To define expected and actual declarations, follow these rules: |
14 |
| - |
15 |
| -1. In the common source set, declare a standard Kotlin construct. This can be a function, property, class, interface, |
16 |
| - enumeration, or annotation. |
17 |
| -2. Mark this construct with the `expect` keyword. This is your _expected declaration_. These declarations can be used in the |
18 |
| - common code, but shouldn't include any implementation. Instead, the platform-specific code provides this implementation. |
19 |
| -3. In each platform-specific source set, declare the same construct in the same package and mark it with the `actual` |
20 |
| - keyword. This is your _actual declaration_, which typically contains an implementation using platform-specific libraries. |
21 |
| - |
22 |
| -During compilation for a specific target, the compiler tries to match each _actual_ declaration it finds with the |
23 |
| -corresponding _expected_ declaration in the common code. The compiler ensures that: |
24 |
| - |
25 |
| -* Every expected declaration in the common source set has a matching actual declaration in every platform-specific |
26 |
| - source set. |
27 |
| -* Expected declarations don't contain any implementation. |
28 |
| -* Every actual declaration shares the same package as the corresponding expected declaration, such as `org.mygroup.myapp.MyType`. |
29 |
| - |
30 |
| -While generating the resulting code for different platforms, the Kotlin compiler merges the expected and actual |
31 |
| -declarations that correspond to each other. It generates one declaration with its actual implementation for each platform. |
32 |
| -Every use of the expected declaration in the common code calls the correct actual declaration in the |
33 |
| -resulting platform code. |
34 |
| - |
35 |
| -You can declare actual declarations when you use intermediate source sets shared between different target platforms. |
36 |
| -Consider, for example, `iosMain` as an intermediate source set shared between the `iosX64Main`, `iosArm64Main`, |
37 |
| -and `iosSimulatorArm64Main`platform source sets. Only `iosMain` typically contains the actual declarations and not the |
38 |
| -platform source sets. The Kotlin compiler will then use these actual declarations to produce the resulting code for the |
39 |
| -corresponding platforms. |
40 |
| - |
41 |
| -The IDE assists with common issues, including: |
42 |
| - |
43 |
| -* Missing declarations |
44 |
| -* Expected declarations that contain implementations |
45 |
| -* Mismatched declaration signatures |
46 |
| -* Declarations in different packages |
47 |
| - |
48 |
| -You can also use the IDE to navigate from expected to actual declarations. Select the gutter icon to view actual |
49 |
| -declarations or use [shortcuts](https://www.jetbrains.com/help/idea/navigating-through-the-source-code.html#go_to_implementation). |
50 |
| - |
51 |
| -{width=500} |
| 3 | +* goal |
| 4 | + * language mechanism of expected and actual declarations |
| 5 | + |
| 6 | +* Expected and actual declarations |
| 7 | + * allow |
| 8 | + * from Kotlin Multiplatform modules -- access -- platform-specific APIs |
| 9 | + * == use platform-agnostic APIs | common code |
| 10 | + |
| 11 | +* see [Use platform-specific APIs](https://www.jetbrains.com/help/kotlin-multiplatform-dev/multiplatform-connect-to-apis.html) |
| 12 | + |
| 13 | +## Rules -- for -- defining expected and actual declarations |
| 14 | + |
| 15 | +* rules for defining |
| 16 | + 1. | common source set, declare a standard Kotlin construct (function, property, class, interface, |
| 17 | + enumeration, or annotation) |
| 18 | + 1. NOT include any implementation -- Reason: 🧠implementation | platform-specific code 🧠 -- |
| 19 | + 2. Mark this construct with the `expect` keyword |
| 20 | + 1. 👀== _expected declaration_ 👀 |
| 21 | + 3. | EACH platform-specific source set's SAME package, declare the same construct & mark it with the `actual` |
| 22 | + keyword |
| 23 | + 1. 👀== _actual declaration_ 👀 |
| 24 | + 2. implementation -- via -- platform-specific libraries |
| 25 | + 4. | intermediate source sets / shared between different target platforms, declare `actual` declarations |
| 26 | + |
| 27 | +* intermediate source set |
| 28 | + * == intermediate between shared source set -- & -- platform source set |
| 29 | + * contain |
| 30 | + * actual declarations |
| 31 | + * _Example:_ `/iosMain` == intermediate source set shared between the `iosX64Main`, `iosArm64Main`, |
| 32 | +-- & -- `iosSimulatorArm64Main`platform source sets |
| 33 | + |
| 34 | +* IDE |
| 35 | + * highlight if |
| 36 | + * Missing declarations |
| 37 | + * Expected declarations / contain implementations |
| 38 | + * Mismatched declaration signatures |
| 39 | + * Declarations | different packages |
| 40 | + * enable navigate from expected -- to -- actual declarations |
| 41 | + |
| 42 | +  |
| 43 | + |
| 44 | +* how does it work? |
| 45 | + * | compilation |
| 46 | + * / specific target, |
| 47 | + * compiler tries to match EACH _actual_ declaration -- with the corresponding -- _expected_ declaration | common code / ensures that |
| 48 | + * EVERY expected declaration | common source set -- has a matching -- actual declaration | EVERY platform-specific source set |
| 49 | + * Expected declarations do NOT contain any implementation |
| 50 | + * EVERY actual declaration's package == corresponding expected declaration's package |
| 51 | + * 👀Kotlin compiler -- merges the -- expected & actual declarations / correspond to each other 👀 |
| 52 | + * -> 💡generates 1 declaration with its actual implementation / each platform 💡 |
| 53 | + * EVERY use of the expected declaration | common code -- calls the -- correct actual declaration | |
| 54 | +resulting platform code |
52 | 55 |
|
53 | 56 | ## Different approaches for using expected and actual declarations
|
54 | 57 |
|
55 |
| -Let's explore the different options of using the expect/actual mechanism to solve the problem of accessing |
56 |
| -platform APIs while still providing a way to work with them in the common code. |
| 58 | +* goal |
| 59 | + * options of using the expect/actual mechanism -- to -- |
| 60 | + * access platform APIs & |
| 61 | + * work with them | common code |
57 | 62 |
|
| 63 | +* TODO: |
58 | 64 | Consider a Kotlin Multiplatform project where you need to implement the `Identity` type, which should contain the user's
|
59 | 65 | login name and the current process ID. The project has the `commonMain`, `jvmMain`, and `nativeMain` source sets to make
|
60 | 66 | the application work on the JVM and in native environments like iOS.
|
|
0 commit comments