Fields can be defined at any place within the declaration of a class or record. To avoid ambiguity with other modifiers, the
var keyword can be used to start an explicit field section, but it is not required.
type MyClass = public class private fValue: String; fNumber1, fNumber2, fNumber3: Integer; method Foo; var override: Boolean; // 'var' keeps 'override' from being ambiguous end;
Each field can be declared as a name/type pair separated by a colon; multiple fields of the same type can also be declared as a comma-separated list, without having to repeat the type name every time.
Fields can be assigned an initial value right in their declaration by having the field declaration closed off with the
:= operator followed by an expression.
var fName: String := 'Paul';
Initialization is only supported for individually declared fields, not when multiple fields are declared with a comma-separated list. If the type of the variable can be inferred from the initialization expression, the explicit type can be omitted:
var fName := 'Paul'; // String is inferred
Please also refer to the Constructors: Initializers topic for more detail on when and how fields get initialized.
Fields can be marked with the
readonly Member Modifier to become read-only.
Storage Modifiers (Cocoa)
var fValue: weak String;
To specify a Storage Modifier, the type cannot be inferred, but must be explicitly specified. Inferred types will always be considered
It is strongly encouraged to keep all fields private, and use properties to expose the class state externally.
Like most type members, fields are by default defined on the instance – that means the fields can be called on and will execute in the context of an instance of the class. A fields can be marked as static by prefixing the fields declaration with the
class keyword, or by applying the
static Member Modifier:
class var fName: String; // static field on the class itself var fName2: String; static; // also a static field on the class itself
A number of other Member Modifiers can be applied to fields.
ISomeInterface(See Explicit Interface Implementations)