HaxeDoc2

(やまだぃちぅ) #1
3.If the field overrides (4.3.1) a public field.

Trivia: Protected
Haxe has no notion of aprotectedkeyword known from Java, C++ and other object-
oriented languages. However, itsprivatebehavior is equal to those language’s protected
behavior, so Haxe actually lacks their real private behavior.

4.4.2 Inline.........................................


Theinlinekeyword allows function bodies to be directly inserted in place of calls to them. This
can be a powerful optimization tool, but should be used judiciously as not all functions are good
candidates for inline behavior. The following example demonstrates the basic usage:
1 class Main {
2 static inline function mid(s1:Int, s2:Int){
3 return (s1 + s2) / 2;
4 }
5
6 static public function main() {
7 var a = 1;
8 var b = 2;
9 var c = mid(a, b);
10 }
11 }


The generated Javascript output reveals the effect of inline:
1 (function () { "use strict";
2 var Main = function() { }
3 Main.main = function() {
4 var a = 1;
5 var b = 2;
6 var c =(a + b) / 2;
7 }
8 Main.main();
9 })();
As evident, the function body(s1 + s2) / 2of fieldmidwas generated in place of the
call tomid(a, b), withs1being replaced byaands2being replaced byb. This avoids a
function call which, depending on the target and frequency of occurrences, may yield noticeable
performance improvements.
It is not always easy to judge if a function qualifies for being inline. Short functions that have
no writing expressions (such as a=assignment) are usually a good choice, but even more com-
plex functions can be candidates. However, in some cases inlining can actually be detrimental
to performance, e.g. because the compiler has to create temporary variables for complex expres-
sions.
Inline is not guaranteed to be done. The compiler might cancel inlining for various reasons
or a user could supply the--no-inlinecommand line argument to disable inlining. The only
exception is if the class is extern (6.2) or if the class field has the:externmetadata (6.9), in
which case inline is forced. If it cannot be done, the compiler emits an error.
It is important to remember this when relying on inline:
Free download pdf