6.12 Inline Constructors

since Haxe 3.1.0

If a constructor is declared to be inline, the compiler may try to optimize it away in certain situations. For this to work, the compiler must be able to verify that the object can be replaced by a set of local variables.

  • The newly-constructed instance must only be accessible from the local function.
  • All called instance functions must be inlined.
  • All other references to the instance must only read or write its variables.

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);
  }
}

A look at the JavaScript output reveals the effect:

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