Readonly (keyword)

From The Oxygene Language Wiki
Jump to: navigation, search

This is a Language topic about Oxygene
 

Language Topics Introduction | Structured Overview | Grammar | Keywords | Functions


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

Personal tools
Namespaces

Variants
Actions
Navigation
Getting Started
Sections
If you know
More
Toolbox