I love @:enum abstacts. But what I do not appriciate is the concept of having very often a small list of enums (let’s say four) that is represented with a 32-bit/ 64 integer.
I then have experimented with an @:enum abstract that has an underlying type of a vector storing booleans.
The vectors are set up to store only two booleans, so we have a four-bit-constellation.
But what I do not appriciate is the concept of having very often a small list of enums (let’s say four) that is represented with a 32-bit/ 64 integer.
Why not? Maybe I’m missing the point, but what is the advantage over just this:
class Test {
static function main() {
var fourBitEnumAbstract:FourBitEnumAbstract = FourBitEnumAbstract.blackBlue;
trace( fourBitEnumAbstract );
}
}
@:enum abstract FourBitEnumAbstract ( Int ) {
var redBlue = 1;
var blackBlue = 2;
var blackBlack = 3;
var redBlack = 4;
@:to public inline function toString() : String {
return switch (cast this:FourBitEnumAbstract) {
case redBlue: "redBlue";
case blackBlue: "blackBlue";
case blackBlack: "blackBlack";
case redBlack: "redBlack";
}
}
}