Readonly (keyword)

From The Oxygene Language Wiki

Jump to:navigation, search

This is a Language topic
Feel free to add your notes to this topic below.


The readonly keyword is used to create fields that are read-only from any place outside the constructor (or class constructor). These fields therefore are immutable and are useful when writing thread-safe classes. Instead of changing the values of the fields on immutable classes, new classes are created with the new values.


The "readonly" directive

type
  MyClass = class
    var i: Integer; readonly;
    ...
  end;

Readonly classes

type
  ImmutableClass = public readonly class
  public 
    property Name: string;
    property Address: string;
    constructor(aName, anAddress: string);
  end;


constructor ImmutableClass(aName, anAddress: string);
begin
  Name := aName;
  Address := anAddress; 
end;

Adding the readonly keyword to a class makes all fields and properties with implicit fields read-only. They can only be set from the constructor. This feature aids in writing immutable classes.

Applies to

See Also


Oxygene-48.png

Area: Oxygene Language
Compiler version: Oxygene 5

Language GlossaryKeywordsTypesFAQHow To

Navigation
Areas
More
Toolbox