Properties
From The Oxygene Language Wiki
This is a Language topic
Feel free to add your notes to this topic below.
Properties provide a useful abstraction for values that can be held in fields or handled by get/set methods:
fMyInt: Integer; fMyProperty: String; property MyInt: Integer read fMyInt write fMyInt; property MyString: String read GetString write SetString; method GetString: String; method SetString(aString: String); public property MyProperty: String read fMyProperty protected write fMyProperty;
A simplified syntax is provided, with the supporting field created for you:
property MyString: String;
Property Modifiers
Inline Expressions
Any expression may be used for the 'read' specification provided that it is of the correct type. For example, it can be as simple as:
property AlwaysTrue: Boolean read true;
Other examples:
property Prop1: Integer read 1 + 3; property Prop2: String read Test.ToString;
Note: Inline expressions relate to 'read' only and 'write' requires a field or method call.
Inline expressions are one of Delphi Prism's big features as:
- the code is more intuitive,
- there is less code to write (often no getter method is needed and usually no explicit field is required),
- maintenance is reduced.
Consider the following example:
Blocks = class public property Area: double read Length*Width; property Length: double; property Width: double; end;
Without Delphi Prism's inline expressions and implicit fields, the following definition would be needed:
Blocks = class private fLength: double; fWidth: double; method GetArea: double; public property Area: double read GetArea; property Length: double read fLength write fLength; property Width: double read fWidth write fWidth; end;
And, of course, a method implementation is needed as well:
method Blocks.GetArea: double; begin Result := fLength * fWidth; end;
Other Property Syntax
Class properties and initial values:
TestClass = class class property Test: Integer; // implicit var class property TestDV: Integer := 15; //implicit var + initial value property STestDV: Integer := 15;
Accessing records:
type TZ = Record a: Integer; end; class p: TZ; property MyP: Integer read p.a write p.a;
Simple boolean:
property TestResult: Boolean read False; // just return false
Your first thought might be "what is the use of that", but consider the following:
property Test: Boolean read {$IFDEF XYZ} False {$ELSE} True {$ENDIF};
At times, nested conditional code can get messy and hard to read, so it becomes error-prone. It can be useful to simplify sometimes, even at the expense of a runtime test.
Properties can be initialized within a constructor call - see Extended Constructor Calls.
Property Access Modifiers
It is possible to have a different access modifier for a property through the use of access specifiers:
public property Value: string read fValue protected write fValue;
One of the read or write has to match the current access modifier. This makes reading a public operation, while writing is for sub-classes only.
Properties in Interfaces
The Delphi Prism language allows you to define properties in interfaces with implementations provided later by implementing classes.
A property declaration inside an interface is shortened to omit a getter/setter method or variable and is reduced to the base property declaration together with "read", "write" or both specifiers:
property One: Integer read; property Two: Integer write; property Three: Integer read write;
Property Attributes
Applying custom attributes to just the read or write method, or applying them to the automatically generated field can be done with the read:, write: or var: prefix. See Attribute Scopes for more information.
Notes
C# and other languages cannot access named indexers, only the default one.
Delphi Prism will always have a hidden get_ and set_ method for your property accessors unless you use get_PropertyName or set_PropertyName in the read/write matching the signature of the property.
See Also
Area: Oxygene Language
Compiler version: Oxygene 5
Language Glossary — Keywords — Types — FAQ — How To