0Pricing
Blockchain Smart Contracts with Solidity · レッスン

構造体と列挙型

Solidityのカスタムstruct型で複雑なデータをモデル化し、enumで状態の固定された集合を表現します。

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

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

Why Custom Types?

Beyond primitives like uint and address, Solidity lets you define your own types. Structs group related fields, and enums represent a fixed set of named states.

Declaring a Struct

A struct bundles several variables under one name, perfect for modeling real-world entities like a user or an order.

struct User {
    string name;
    uint age;
    address wallet;
}

Creating Struct Instances

You can construct a struct by positional arguments or by naming each field. Named arguments are clearer and less error prone.

User memory u = User({
    name: "Alice",
    age: 30,
    wallet: msg.sender
});

Structs in Storage vs Memory

A struct in storage persists on chain; in memory it is a temporary copy. Assigning a storage struct to a memory variable copies it, so changes do not write back.

Updating Struct Fields

To modify persisted data, work with a storage reference and assign to its fields directly.

User storage u = users[msg.sender];
u.age = 31;

Structs Inside Mappings

Structs shine when stored in a mapping keyed by address or id, giving each user their own record.

mapping(address => User) public users;
users[msg.sender] = User("Bob", 25, msg.sender);

Declaring an Enum

An enum defines a small set of named options. It improves readability over raw numbers for representing state.

enum Status { Pending, Shipped, Delivered, Cancelled }

Using Enum Values

Enum members are stored as uint starting at 0. You compare and assign them by name, which makes contract logic self-documenting.

Status public status = Status.Pending;

function ship() public {
    status = Status.Shipped;
}

Enums in State Machines

Enums are ideal for modeling a state machine. Guard transitions with require so an order can only move to a valid next state.

function deliver() public {
    require(status == Status.Shipped, "Not shipped yet");
    status = Status.Delivered;
}

Combining Structs and Enums

Put an enum field inside a struct to track each record state, for example storing a Status on every order struct so each order tracks its own lifecycle.

Gas Considerations

Each struct field is a storage slot, and writing storage is expensive. Order fields to pack smaller types together, and use enums (one byte range) instead of strings for fixed states to save gas.

Quick Check

Check your structs and enums knowledge.

Recap

You learned Solidity custom types:

  • Structs group related fields and are often stored in mappings
  • Mind storage vs memory when copying or updating structs
  • Enums model fixed states as integers and power state machines
  • Combine them and pack fields to save gas

Custom types make contracts more expressive and maintainable.

よくある質問

「構造体と列挙型」レッスンは無料ですか?

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

「構造体と列挙型」で何を学びますか?

Solidityのカスタムstruct型で複雑なデータをモデル化し、enumで状態の固定された集合を表現します。 ブラウザで直接実行するハンズオンコードでBlockchain Smart Contracts with Solidityを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Blockchain Smart Contracts with Solidityを始めるのに経験は必要ですか?

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

「構造体と列挙型」レッスンにはどのくらい時間がかかりますか?

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

このBlockchain Smart Contracts with Solidityレッスンでコードを書いて実行できますか?

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

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

  1. Solidityのデータ型と変数
  2. 制御構文とループ
  3. 関数と可視性修飾子
  4. 構造体と列挙型
← Blockchain Smart Contracts with Solidityに戻る