COMMUNITY

How to compare enum as int in haxe4?

how to compare enum as int in haxe4?

///----------------------------

@:enum
abstract CardType (Int)
{
var CT_ERROR = 1;
var CT_SanPai= 2;
var CT_SanGong= 3;
}

///

var cbFirstCardType = CardType.CT_ERROR;
var cbNextCardType = CardType.CT_SanGong;

		if (cbFirstCardType != cbNextCardType) 
{
	if (cbNextCardType > cbFirstCardType) return 1;
	else return -1;
}

// Cannot compare gameLogic.CardType and gameLogic.CardType

how to fixed that ?

You need to forward the operator. See also: https://stackoverflow.com/a/48129137/2631715

why not add features like c#

Enum–>Int

(int)Colors.Red, (byte)Colors.Green

I still don’t know how to compare add @:forward to compare?

can you show me a little examples?

@Gama11

@:enum
abstract CardType (Int)
{
var CT_ERROR = 1;
var CT_SanPai= 2;
var CT_SanGong= 3;
  
  @:op(A < B) static function lt(a:CardType, b:CardType):Bool;
  @:op(A <= B) static function lte(a:CardType, b:CardType):Bool;
  @:op(A > B) static function gt(a:CardType, b:CardType):Bool;
  @:op(A >= B) static function gte(a:CardType, b:CardType):Bool;
  @:op(A == B) static function eq(a:CardType, b:CardType):Bool;
  @:op(A != B) static function ne(a:CardType, b:CardType):Bool;
}

http://try-haxe.mrcdk.com/#E6d99

@RealyUniqueName can it default add to when check type (Int,float) ? or add something else like

@:enum @:compare instead of add lot’s line

@:op(A < B) static function lt(a:CardType, b:CardType):Bool;
@:op(A <= B) static function lte(a:CardType, b:CardType):Bool;
@:op(A > B) static function gt(a:CardType, b:CardType):Bool;
@:op(A >= B) static function gte(a:CardType, b:CardType):Bool;
@:op(A == B) static function eq(a:CardType, b:CardType):Bool;
@:op(A != B) static function ne(a:CardType, b:CardType):Bool;

@Zhan Yes, here you go

@:enum abstract TFoo(Int) to Int {
 var a = 0;
 var b = 1;
 var c = 2;
}

trace(a==b, a == c, a == a);
//false false true

This can be achieved with a build macro

Won’t a simple typecast work?

However – as a design notion, I really prefer to see explicit code which handles things like this, so that (a) I can plainly see that this is a comparison that the rest of the program is interested in, and (b) I have “just one place” at which to change the [entire …] program’s behavior if it needs to be changed.

“Okay, so this enum is ‘ordered.’” All of the enumeration values happen to correspond to integers that can be meaningfully compared with operators like >, and the correct interpretation of that operator is, right now, identical to that of “the underlying integers.” Now, here comes Private SNAFU – a change, beyond your control (say in an outside API …) throws a monkey-wrench. Now you’re running all over the code doing search-and-replace or worse. (Hope you don’t miss a single one, because you’re now screwed if you do.)

I prefer to treat enums as fairly-abstract types … and, “not ordinal.” I prefer to put comparison and other operations into clearly-defined (even if “redundant”) pieces of code which document the behavior while providing a single point of change.