0Pricing
Mojo Academy · レッスン

条件付きコンパイル

コンパイル時にコードパスを分岐します

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

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

Choosing Code at Compile Time

Conditional compilation picks which code path to build based on compile-time values, so the unused branch never reaches the final program. ⚙️

The @parameter if Statement

Add @parameter to an if whose condition is known at compile time, and Mojo keeps only the branch that matches.

@parameter
if width == 1:
    scalar_path()
else:
    vector_path()

The Condition Must Be Static

The test has to use parameters or aliases, values the compiler already knows, not data that only appears while the program runs.

alias DEBUG = False

@parameter
if DEBUG:
    print("tracing")

Dead Branches Disappear

The branch that fails the condition is simply not compiled, so it adds no size and costs nothing at runtime.

Specialize by Type

Use it to switch logic based on a type parameter, choosing the best implementation for floats versus integers, for example.

@parameter
if T.is_floating_point():
    use_fma()
else:
    use_int_mul()

Branch on Hardware Facts

Pair conditional compilation with platform checks to emit the right kernel for the CPU or accelerator you are targeting.

Feature Flags Without Cost

Gate optional features behind an alias flag; when it is False the whole feature compiles away, leaving a lean binary.

Different From a Runtime if

A normal if always keeps both branches in the binary and tests at runtime; @parameter if decides once, at build time, and drops the rest.

Combine With Other Metaprogramming

Conditional compilation mixes well with parameterized functions and unrolled loops, letting each specialization take its own optimal path.

One Source, Many Targets

From a single definition you generate tailored builds for each platform or type, with no runtime branching tax to pay. 🚀

Keep Both Paths Valid

Each branch must still compile on its own when chosen, so write every path to be correct for the parameter values that select it.

Quick Check

Compare @parameter if with a normal if.

Recap

@parameter if chooses code paths at compile time: dead branches vanish, you specialize by type or platform, and the binary stays lean. 🎯

よくある質問

「条件付きコンパイル」レッスンは無料ですか?

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

「条件付きコンパイル」で何を学びますか?

コンパイル時にコードパスを分岐します ブラウザで直接実行するハンズオンコードでMojo Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

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

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

「条件付きコンパイル」レッスンにはどのくらい時間がかかりますか?

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

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

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

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

  1. パラメトリックアルゴリズム
  2. コンパイル時のループアンローリング
  3. 条件付きコンパイル
  4. 制約と静的チェック
← Mojo Academyに戻る