Skip to content

Latest commit

 

History

History
48 lines (34 loc) · 1.25 KB

6.11.内联构造函数.md

File metadata and controls

48 lines (34 loc) · 1.25 KB

6.11.内联构造函数

Haxe 3.1.0 以后

Since Haxe 3.1.0

如果一个构造函数声明为 内联(第4.4.2),编译器在某些情况下会尝试优化它。有下面几个需要:

If a constructor is declared to be inline (4.4.2), the compiler may try to optimize it away in certain situations. There are several requirements for this to work:

  • 构造函数调用的结果必须直接分配到一个局部变量
  • 构造函数字段的表达式必须只包含给它的字段的赋值。
  • The result of the constructor call must be directly assigned to a local variable.
  • The expression of the constructor field must only contain assignments to its fields.

下面的例子演示了内联构造函数:

The following example demonstrates constructor inlining:

class Point { 
    public var x:Float; 
    public var y:Float; 
    
    public inline function new(x:Float, y:Float) { 
        this.x = x; 
        this.y = y; 
    } 
} 

class Main { 
    static public function main() { 
        var pt = new Point(1.2, 9.3); 
    } 
} 

看一下JavaScript的输出,揭示了效果:

A look at the JavaScript output reveals the effect:

Main.main = function() { 
    var pt_x = 1.2; 
    var pt_y = 9.3; 
};