FIELD
A FIELD is one named, typed member of a record
TYPE.
Format
The grammar of a single FIELD line:
FIELD ::= "FIELD" type identifier [ "=" constantExpression ]
type ::= name [ "(" type { " " type } ")" ]
constantExpression ::= constant | name { " " constant }
constant ::= string | integer | number
name ::= identifier { "." identifier }
identifier ::= letter { letter | digit }
FIELD keyword is followed by the field's type and then its name. It may end with an
optional = and a constant expression — a constant, or a type constructor
applied to constants — giving the field a default value.Usage
A FIELD keyword can only live inside a
TYPE keyword.
Inside a TYPE
Declares one named, typed member of the record — its type, then its name.
TYPE Point
FIELD Maths.Real x
FIELD Maths.Real y
Each field of a record is one indented FIELD line; a type built from fields is a record,
and together they define both its members and, in declaration order, the positional arguments of its
constructor — so Point 0.0 0.0 fills x then y. A type holds
fields or options, never both.
Can Contain
Nothing. A FIELD is a leaf: a type and a name on one line. It has no body and opens no
indented lines — it only adds one member to the record it sits in.
Targets
The same example, rendered to each target. Each FIELD becomes one member of the
target's idiomatic struct or class, and Maths.Real resolves to that target's
real-number primitive.
JavaScript
class Point {
constructor(x, y) { this.x = x; this.y = y; }
}
TypeScript
class Point {
constructor(public x: number, public y: number) {}
}
Dart
final class Point {
final double x;
final double y;
Point(this.x, this.y);
}
Swift
public struct Point {
public let x: Double
public let y: Double
public init(_ x: Double, _ y: Double) { self.x = x; self.y = y }
}
Go
type Point struct {
x float64
y float64
}
Rust
struct Point {
x: f64,
y: f64,
}
Zig
const Point = struct {
x: f64,
y: f64,
};
JSON
["D", "Point", [], [
["P", "x", "Maths.Real"],
["P", "y", "Maths.Real"]
]]
Related
A type built from fields is a record; the union counterpart of a field is an
OPTION — where a record is every field
at once, a union is exactly one of several options, and both are declared with
TYPE. A field whose value may be absent is typed as
an Optional rather than marked with a suffix. Back to
the language.