Structs and enums
In the terminalela --spec structs-and-enums
- A struct is a plain grouping of typed fields. No classes, no methods.
- Fields are read and set with a dot; a misspelled field is an error.
- Enum values are referenced with a dot.
- A short one goes on its line, the fields or values
;apart:struct Point; x: number; y: number,enum Mode; DEBUG; RELEASE(Statements and blocks). Struct and enum names are Capitalised, and enum members UPPER_CASE, since they are constants; fields are snake_case (Naming style). - A struct or an enum is its declaration: an imported file's
Pand the script'sPare two types, so a parameter typedPtakes the script's alone, and==on one of each is an error.
struct Server host: string port: numberend struct enum Mode DEBUG RELEASEend enum s = Server{host: "models.example", port: 8080}echo $s.hostmode = Mode.RELEASE
A field whose type ends in ? is optional: it may hold null, and a struct literal may leave it out, which leaves it null. A left-out field and one set to null are the same. has($s, "port") says whether it has a value; assigning a value sets it, and assigning null clears it. Reading a null field gives null, which refuses any use but a comparison (null).
struct Server host: string port: number?end struct s = Server{host: "models.example"}if has($s, "port") echo "port $s.port"end ifs.port = 8080