Properties simulate the behavior of fields by two access methods: the first method should return the current property value, and the second one, must assign a new value to the property.
A property is declared like any field, except the declaration must start with the property keyword:
property Re: Double; property Im: Double;
You can declare together several properties when they belong to the same type:
property Re, Im: Double;
You can add the readonly modifier after the declaration if you want a read only property:
property Rho: Double; readonly;
A property declaration can also include a method modifier.
property FullName: String; virtual;
You must supply two access methods for implementing a read/write property. A read only property requires only one access method:
implementation for Complex is var fRe, fIm: Double; property Rho: Double; begin Result := Math.Sqrt(fRe * fRe + fIm * fIm); end; property Re: Double; begin Result := fRe; end; property Re(Value: Double); begin fRe := Value; end; // ...
Both access methods must be declared with the keyword property instead of method. The read method must return a value from the property's type, and it cannot have parameters. The write method cannot have a return type, and it must have a single input parameter declared with the property's type. You can name this parameter as you like, but it's a good practice to name it Value.
If you declare a property but you provide no implementation for it, the compiler assumes your property has an automatic field-based implementation.
Stack = class[X] public property Count: Integer; readonly; property Title: String; end;
Please note that, though Count is declared read only, you can use it inside Stack's methods as a read/write field.
Field-based properties can be initialized with an initialization expression:
Stack = class[X] public // ... property Title: String := "Don't touch my stack!"; end;
When a read only property cannot be implemented as a field-based property, you still can define it as a expression-based property, with a conveniently short alternative syntax. For instance, the Rho property from the previously shown Complex example, could have been defined and declared this way:
property Rho: Double => Math.Sqrt(fRe * fRe + fIm * fIm);
The above declaration could have been used to directly define the property in the declaration section of the type. In this context, the readonly modifier is not accepted, since its presence is assumed.
Expression-based implementations are also available for operators and methods.
You are not required to provide a property declaration for a private property. Instead, you may only write the corresponding access methods, one for readonly properties and two for read/write properties, inside the implementation section of a class, and the compiler will automatically generate the corresponding private declaration.
The Freya Programming Language
Indexers
Type declarations
Type members
Constructors
Methods
User defined operators
Iterators
Fields
Events