HaxeDoc2

(やまだぃちぅ) #1

10 function set_x(x){
11 return this.x = x;
12 }
13
14 static public function main() {}
15 }


If a physical field is indeed intended, it can be forced by attributing the field in question with
the:isVarmetadata (6.9):
1 class Main {
2 // @isVar forces the field to bephysical
3 // allowing the program to compile.
4 @:isVar public var x(get, set):Int;
5
6 function get_x() {
7 return x;
8 }
9
10 function set_x(x){
11 return this.x = x;
12 }
13
14 static public function main() {}
15 }


Trivia: Property setter type
It is not uncommon for new Haxe users to be surprised by the type of a setter being required
to beT->Tinstead of the seemingly more naturalT->Void. After all, why would asetter
have to return something?
The rationale is that we still want to be able to use field assignments using setters as right-
side expressions. Given a chain likex=y=1, it is evaluated asx = (y = 1). In order
to assign the result ofy=1tox, the former must have a value. Ifyhad a setter returning
Void, this would not be possible.

4.3 Method............................................


While variables (4.1) hold data, methods are defining behavior of a program by hosting expres-
sions ( 5 ). We have seen method fields in every code example of this document with even the
initial Hello World (1.3) example containing amainmethod:
1 class HelloWorld {
2 static public function main():Void{
3 trace("Hello World");
4 }
5 }
Methods are identified by thefunctionkeyword. We can also learn that they

1.have a name (here:main),

2.have an argument list (here: empty()),
Free download pdf