0Pricing
Mojo Academy · レッスン

役立つ型アノテーション

型を追加してMojoの最適化とエラー検出に役立てます

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

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

What Is a Type Annotation?

A type annotation states a value's type right in the declaration, so both you and Mojo know exactly what it holds.

Annotating a var

Add a colon and the type after the name. Here : Int promises that count will only ever hold whole numbers.

var count: Int = 0

Inference vs Annotation

Without an annotation Mojo infers the type from the value. Writing it yourself makes your intent explicit and clear.

var x = 5
var y: Int = 5

Catching Mistakes Early

An annotation lets Mojo reject a wrong value at compile time. Assigning text to an Int becomes an immediate error.

var count: Int = "oops"  # error

Annotations Aid Speed

Knowing the exact type ahead of time, Mojo can generate faster machine code instead of checking types as it runs.

Annotating Floats and Bools

Annotations work for every type. Spell out Float64 or Bool to document precisely what each value is.

var ratio: Float64 = 0.5
var ok: Bool = True

Annotating Strings

Mark text values with String so readers instantly see that a var carries words rather than numbers.

var name: String = "Ada"

Annotations as Documentation

A type next to a name tells the next reader, including future you, what to expect without hunting for the value.

Required in fn Functions

Strict fn functions require type annotations on their parameters, which is where this habit pays off most.

fn add(a: Int, b: Int):
    print(a + b)

Annotate at Boundaries

Even when inference works, annotate the values others rely on. Clear types at boundaries prevent confusion across your code.

Types Are a Safety Net

Annotations turn silent bugs into loud errors and unlock speed. A little typing now saves real debugging later.

Quick Check

You annotate a var as Int but try to assign it the text "oops". What happens in Mojo?

Recap

A type annotation names a value's type with a colon, catching errors early and helping Mojo run faster. 🎯

よくある質問

「役立つ型アノテーション」レッスンは無料ですか?

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

「役立つ型アノテーション」で何を学びますか?

型を追加してMojoの最適化とエラー検出に役立てます ブラウザで直接実行するハンズオンコードでMojo Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

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

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

「役立つ型アノテーション」レッスンにはどのくらい時間がかかりますか?

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

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

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

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

  1. varによる値の宣言
  2. Int、Float、Bool
  3. 文字列とテキスト
  4. 役立つ型アノテーション
← Mojo Academyに戻る