0Pricing
Kotlin Multiplatform Academy · レッスン

expect/actualが存在する理由

1つのAPIの背後でプラットフォーム固有の動作を実現する仕組みを学びます

「expect/actualが存在する理由」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはKotlin Multiplatform Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

One Codebase, Different Worlds

Most of your Kotlin runs everywhere, but some tasks need each platform's own tools. That gap is exactly what expect/actual was built to close.

The Problem It Solves

Getting the OS version, a file path, or current time relies on Android or iOS APIs that simply do not exist in shared code. You still want to call them from commonMain. 🤔

A Promise and a Fulfillment

Think of expect as a promise: common code declares an API exists. Then each platform fulfills that promise with a real implementation.

The expect Declaration

In commonMain you write a declaration with no body. The expect keyword means the implementation lives elsewhere, per platform.

expect fun platformName(): String

The actual Implementation

Each target supplies the body with actual. Android returns one value, iOS returns another, but the signature must match exactly.

actual fun platformName(): String = "Android"

Common Code Stays Common

Your shared logic just calls the function and never sees the difference. The compiler picks the right actual for each build.

val label = "Running on " + platformName()

Not the Same as an Interface

An interface needs an object to implement it at runtime. expect/actual is resolved at compile time, so there is zero runtime cost.

Where Each Piece Lives

The expect goes in commonMain. Every actual lives in its matching source set, like androidMain or iosMain, side by side.

Every Target Must Answer

If you ship to Android and iOS, both need an actual. A missing one is a compile error, not a surprise crash later.

Use It Sparingly

Reach for expect/actual only for the thin layer that truly differs. The more code stays in common, the more you share. ✨

A Bridge, Not a Wall

expect/actual is a clean bridge to native power without splitting your project. You keep one API while each platform speaks its own language.

Quick Check

Let's make sure the core idea landed.

Recap

You learned why expect/actual exists: it lets common code call platform-specific behavior through one API, resolved at compile time with no runtime cost. 🎉

よくある質問

「expect/actualが存在する理由」レッスンは無料ですか?

はい。「expect/actualが存在する理由」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。

「expect/actualが存在する理由」で何を学びますか?

1つのAPIの背後でプラットフォーム固有の動作を実現する仕組みを学びます ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Kotlin Multiplatform Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。

「expect/actualが存在する理由」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?

はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. expect/actualが存在する理由
  2. ターゲットごとにプラットフォーム名を取得する
  3. expect/actualのクラスとプロパティ
  4. よくあるミスとコンパイラーエラー
← Kotlin Multiplatform Academyに戻る