Haxe has a unique way of using getter/setter functions and it can be sometimes confusing for developers coming from other languages.
I recommend to read the official documentation as well.
Getter and Setter
You can define a variable to have getter and setter functions using get
and set
keywords as shown below.
The functions names should be prefixed with get_
variable name and set_
variable name.
The most important thing to note here is you cannot access the physical variable inside your getter and setter functions. You get the error This field cannot be accessed because it is not a real variable
if you try to access amount
inside get_amount
and set_amount
functions.
To access physical variable you need to add metadata @:isVar
in front of the variable amount
.
Getter (read-only) or Setter (write-only)
By using null
in place of set
you can mark the variable read-only with getter function as shown below.
Similarly by using null
in place of get
you can mark the variable write-only with setter function as shown below.
The variable can still be accessed for reading and writing within the class but not from outside.
Setter with default Getter
By using default
in place of get
you can mark the variable to return it’s value when accessed and still have a setter function.
Quick Reference