Fields

A field is a simple variable in a Class or Record that stores (part of) the type's state.

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.

Initializers

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.

Read-Only Fields

Fields can be marked with the readonly Member Modifier to become read-only.

Read-only fields can still be written to from an Initializer or from the class's Constructors – but they cannot be modified once construction of an instance has completed.

Storage Modifiers (Cocoa)

On the Cocoa platform, the type of a field declaration can be amended with one of the weak, unretained or strong Storage Modifier keywords, with strong being the implied default.

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 strong.

Cocoa Only

Storage Modifiers are necessary on Cocoa only, to assist with Automatic Reference Counting (ARC). Thye can be specified on all platfroms, but have no effect when using GC.

Visibility

The visibility of fields is governed by the Visibility Section of the containing type the fields is declared in, or the Visibility Modifiers applied to the method.

It is strongly encouraged to keep all fields private, and use properties to expose the class state externally.

Static Fields

Like most type members, fields are by default defined on the instance – that means fields can be called on and will execute in the context of an instance of the class. All fields can be marked as static by prefixing the field's 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

Other Modifiers

A number of other Member Modifiers can be applied to fields.

  • deprecated Triggers a deprecation warning when used.
  • external For Island/Toffee class fields; this field is defined in an external module.
  • implements   ISomeInterface (See Explicit Interface Implementations).
  • readonly Readonly fields can only be set from the constructors, after that they are readonly.
  • unsafe Allows the use of unsafe types in the field signature.
  • volatile Volatile ensures access to this field is never optimized.

See Also