haxe icon indicating copy to clipboard operation
haxe copied to clipboard

Enum abstract Integers using exposed operators should be treated as constants.

Open Geokureli opened this issue 4 years ago • 3 comments

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;

Geokureli avatar May 01 '21 23:05 Geokureli

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.

Simn avatar Jan 18 '22 14:01 Simn

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.

Geokureli avatar Jan 18 '22 17:01 Geokureli

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

trethaller avatar Feb 07 '22 15:02 trethaller