Hello,
I was toying around with abstracts and wrote an abstract for an n-element array that will automatically "trim" superfluous elements on cast. Essentially, here's how I go about it:
class Test {
static function main() {
var x : Array2 = [2, 4, 6, 8, 10];
trace(x);
var y : Array2 = [1, 3];
trace(y);
}
}
abstract Array2(Array<Int>) {
public inline function new(x : Int, y : Int) {
this = [x, y];
}
@:from static inline function fromArrayInt(a : Array<Int>) : Array2 {
return new Array2(a[0], a[1]);
}
@:to inline function toArrayInt() : Array<Int> {
return [this[0], this[1]];
}
}
The idea is that only the first two elements of an array that is being cast from is taken into consideration by the
Array2 abstract. However, I noticed that the generated JavaScript code is a bit wordier than I expected it to be.
Here's the JavaScript output generated by Haxe 3.2.1 using the -dce full flag:
(function (console) { "use strict";
var Test = function() { };
Test.main = function() {
var x;
{
var a_0 = 2;
var a_1 = 4;
var a_2 = 6;
var a_3 = 8;
var a_4 = 10;
x = [a_0,a_1];
}
console.log(x);
var y;
{
var a_01 = 1;
var a_11 = 3;
y = [a_01,a_11];
}
console.log(y);
};
Test.main();
})(typeof console != "undefined" ? console : {log:function(){}});
For comparison, I decided to try the same with Haxe 3.2.0. Here's the result:
(function (console) { "use strict";
var Test = function() { };
Test.main = function() {
var tmp;
tmp = [2,4];
var x = tmp;
console.log(x);
var tmp1;
tmp1 = [1,3];
var y = tmp1;
console.log(y);
};
Test.main();
})(typeof console != "undefined" ? console : {log:function(){}});
If you're interested, here it is on
try.haxe.org.
Seems to me that the Haxe 3.2.0 result is much cleaner even though it's using avoidable temporary variables. Is there an advantage to the new (3.2.1) way of code generation? I mean, JavaScript doesn't have a block-level scope, so there doesn't seem to be a reason to format code this way other than readability. For a larger number of array elements, wouldn't the initialization of variables be wasteful? Is there a way to optimize the output without digging into compiler sources?
Sorry if these are all silly questions. I had this idea that an n-element abstract array would end up performing better than a "dedicated" class. Wouldn't surprise me to learn I'm wrong!
Thank you for any thoughts you care to share!
Domagoj