Enum abstract Integers using exposed operators should be treated as constants.
in the example below ANY should be considered a constant
class Test {
static function main() { fooUp(WALL); }
static public function fooUp(
dir1:Directions = NONE, dir2:Directions = WALL,// these work
dir3:Directions = ANY)// Error: Parameter default value should be constant
{
trace(dir1, dir2, dir3);
}
}
@:enum abstract Directions(Int) from Int to Int
{
var NONE = 0x0000;
var LEFT = 0x0001;
var RIGHT = 0x0010;
var UP = 0x0100;
var DOWN = 0x1000;
var WALL = 0x0001 | 0x0010;
var ANY = LEFT | RIGHT | UP | DOWN;
@:op(A | B)
static function or (a:Directions, b:Directions):Directions;
}
observed in 4.2.1, 3.4.7 and whatever the latest dev on try.haxe is.
this also happens when the or operator is defined with normal operator overloading, this should also work.
@:op(A | B)
inline function or(dir:Int):FlxDirections return this | dir;
IMO we should discuss dropping this "constant" restriction entirely. From what I understand, this originally came from limitations the flash run-time had, but I don't see any reason why we should still forbid this.
Flash already has extra restrictions regarding optional or default args, you're only allowed to omit the final args in a call. I see no reason to let flash hold all the other targets back.
That said, I think the example above should be fixed so that it is considered a constant, in case that affects other uses.
Just stumbled across this, interestingly adding | 0 works around the problem:
enum abstract Collision(Int) from Int to Int {
var Flag1 = 1 << 0;
var Flag2 = 1 << 1;
var AllFlags = Flag1 | Flag2 | 0;
}
This allows using AllFlags as default value, whereas without I get the Parameter default value should be constant error