- Have a field which invokes agetter-method upon read-access.
- Have a field which invokes asetter-method upon write-access.
When dealing with properties, it is important to understand the two kinds of access:
Definition: Read Access
A read access to a field occurs when a right-hand side field access expression (5.7) is used.
This includes calls in the form ofobj.field(), wherefieldis accessed to be read.
Definition: Write Access
A write access to a field occurs when a field access expression (5.7) is assigned a value in the
form ofobj.field = value. It may also occur in combination with read access (4.2) for
special assignment operators such as+=in expressions likeobj.field += value.
Read access and write access are directly reflected in the syntax, as the following example
shows:
1 class Main {
2 public var x(default, null):Int;
3 static public function main() { }
4 }
For the most part, the syntax is similar to variable syntax, and the same rules indeed apply.
Properties are identified by
- the opening parenthesis(after the field name,
- followed by a specialaccess identifier(here:default),
- with a comma,separating
- another special access identifier (here:null)
- before a closing parenthesis).
The access identifiers define the behavior when the field is read (first identifier) and written
(second identifier). The accepted values are:
default: Allows normal field access if the field has public visibility, otherwise equal tonull
access.
null: Allows access only from within the defining class.
get/set: Access is generated as a call to anaccessor method. The compiler ensures that the acces-
sor is available.
dynamic: Likeget/setaccess, but does not verify the existence of the accessor field.
never: Allows no access at all.
Definition: Accessor method
Anaccessor method(or shortaccessor) for a field namedfieldof typeTis agetternamed
get_fieldof typeVoid->Tor asetternamedset_fieldof typeT->T.