Inline constructors are indeed a thing of beauty: I will need to look into this some more. This would allow me to avoid creating copies inside functions where I need to do some manipulation math.
What I did notice is that the resulting code looks a bit weird in Haxe 3.4
Let's say we have this Vector3 class:
class Vector3
{
public var X : Float;
public var Y : Float;
public var Z : Float;
public inline function new(x : Float, y : Float, z : Float)
{
this.X = x;
this.Y = y;
this.Z = z;
}
public inline function AddValue(value : Float)
{
this.X += value;
this.Y += value;
this.Z += value;
}
}
How if we would write this code:
static function TestVector(vector : Vector3)
{
var copy = new Vector3(vector.X, vector.Y, vector.Z);
copy.AddValue(10);
}
It gets compile to this. I find it rather strange that copy_Y and copy_Z are assigned later on. I wonder if this is slightly less performant or something in JS, but it does avoid creating an actual copy.
Main.TestVector = function(vector) {
var copy_Z;
var copy_Y;
var copy_X = vector.X;
copy_Y = vector.Y;
copy_Z = vector.Z;
copy_X += 10;
copy_Y += 10;
copy_Z += 10;
};
When enabling the -D analyzer-optimize flag you get this. I find this rather peculiar as one of the goals of the optimizer is to create more compact/less code, right?
Main.TestVector = function(vector) {
var copy_Z;
var copy_Y;
var copy_X;
var x = vector.X;
var y = vector.Y;
var z = vector.Z;
copy_X = x;
copy_Y = y;
copy_Z = z;
copy_X = x + 10;
copy_Y = y + 10;
copy_Z = z + 10;
};