Expand/Shrink

struct

Definition: [global] struct identifier
    ( "<c-string>" | { field [ ',' ... ] } )
end struct
-- or --
struct|identifier variable ( ';' | '=' ( new() | expr ) ) [ ',' ... ]
-- or --
bool res = struct|identifier(object o)
Description: As well as defining new structs, the base struct builtin type can also be used, along with the identifiers it introduces, to declare and instantiate variables of that type, and/or test whether or not some value is of that type. Standard scope rules apply to identifier, specifically prefix with global to make any types and/or variables visible in other files, whereas the individual field names can only be accessed via dot notation syntax (or builtins\structs.e).

For interfacing to C, use a c-struct, ie define the contents using a C string.

See technicalia for additional notes on the mandatory ';' for unassigned struct vars and the optional parameters of new().
pwa/p2js: Not supported.
Example:
struct bridge
    string name = "london"
    integer completed = 1971,
            age = 48
end struct
bridge b = new(),
       t = new({"tower",1894,125})
?b.age
--b.age = "oops" -- type error

In their simplest form, structures are just a collection of fields.
If no default value is specified for a field, one will be provided automatically: 0/false/""/{}/NULL for atom/bool/string/sequence/struct|object respectively.

When you declare struct bridge there is also an implicit type bridge() automatically declared on your behalf.

If you wanted something nice and simple, that’s it.
If you wanted something a bit more powerful and flexible, read on.
Simple structs can in fact also be extended and embedded, as will be explained shortly.
Implementation: DoStruct() in pmain.e (along with several other places in that file) maps the above syntax to routines in builtins\structs.e
Note the latter is "dual use": the compiler itself uses the same routines to lookup what it has already compiled, and of course that same information is also used at run-time, in other words the compiler both calls routines in structs.e and emits the same exact calls to be invoked (again) at run-time, as well as a few more run-time only calls.
See Also: builtins\struct.e:new(), class
Expand/Shrink