HaxeDoc2

(やまだぃちぅ) #1

1 class Main {
2 public static function main () { }
3
4 static function test() {
5 if (Math.random() > 0.5) {
6 return "ok";
7 } else {
8 error("random failed");
9 }
10 }
11
12 static inline function error(s:String){
13 throw s;
14 }
15 }


If the call toerroris inlined the program compiles correctly because the control flow checker
is satisfied due to the inlined throw (5.22) expression. If inline is not done, the compiler only sees
a function call toerrorand emits the errorA return is missing here.

4.4.3 Dynamic.......................................


Methods can be denoted with thedynamickeyword to make them (re-)bindable:
1 class Main {
2 static dynamic function test() {
3 return "original";
4 }
5
6 static public function main() {
7 trace(test()); // original
8 test = function() { return "new";}
9 trace(test()); // new
10 }
11 }


The first call totest()invokes the original function which returns theString "original".
In the next line,testisassigneda new function. This is precisely whatdynamicallows: Func-
tion fields can be assigned a new function. As a result, the next invocation oftest()returns the
String "new".
Dynamic fields cannot beinlinefor obvious reasons: While inlining is done at compile-
time, dynamic functions necessarily have to be resolved at runtime.

4.4.4 Override.......................................


The access modifieroverrideis required when a field is declared which also exists on a parent
class (2.3.2). Its purpose is to ensure that the author of a class is aware of the override as this may
not always be obvious in large class hierarchies. Likewise, havingoverrideon a field which
does not actually override anything (e.g. due to a misspelled field name) triggers an error as
well.
The effects of overriding fields are detailed inOverriding Methods(Section 4.3.1). This mod-
ifier is only allowed on method (4.3) fields.
Free download pdf