Struct ed enum
Modelli dati complessi in Solidity usando tipi struct personalizzati e rappresenti insiemi fissi di stati con gli enum.
Struct ed enum è una lezione Blockchain Smart Contracts with Solidity gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Blockchain Smart Contracts with Solidity, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Blockchain Smart Contracts with Solidity include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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.
Impara Blockchain Smart Contracts with Solidity con un tutor IA — gratis
Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.
- Corsi
- 12
- Lezioni
- 48
Domande Frequenti
La lezione «Struct ed enum» è gratuita?
Sì — il testo completo di «Struct ed enum» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Blockchain Smart Contracts with Solidity, passa a CoddyKit PRO. Il corso Blockchain Smart Contracts with Solidity include 4 lezioni in totale.
Cosa imparerò in «Struct ed enum»?
Modelli dati complessi in Solidity usando tipi struct personalizzati e rappresenti insiemi fissi di stati con gli enum. Eserciti Blockchain Smart Contracts with Solidity con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Blockchain Smart Contracts with Solidity?
Non è richiesta alcuna esperienza precedente. Blockchain Smart Contracts with Solidity su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Struct ed enum»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Blockchain Smart Contracts with Solidity?
Sì. Ogni lezione Blockchain Smart Contracts with Solidity include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Tipi di dati e variabili in Solidity
- Strutture di controllo e cicli
- Funzioni e modificatori di visibilità
- Struct ed enum