hxcpp enum bug

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

hxcpp enum bug

Deano

Hi,

It seems that hxcpp generation has a bug when comparing enum values. If used directly in a ‘if’ it doesn’t happen, but use in a switch and it works.

I’m in the middle of something at the moment, so haven’t produces a minimal test case but my code is basically doing this.

enum ElementType {

                Single;

                Sett;

                Image;

                Page;

                Alias;

}

 

var eleType = Alias;

if( eleType == Alias ) trace(“w00t”);

switch(eleType ) {

   case Alias:

       trace( “yep” );

   default:

}

 

Console:

Yep

 

The ‘if’ line never evaluates to true, looking at the cpp the ‘if’ compares via

(eleType == ::display::ElementType_obj::Alias)

Whilst the switch does

::display::ElementType _switch_2 = (eleType);

switch((_switch_2)->GetIndex()){

 

The cpp ‘if’ code should be generating GetIndex() I believe…

 

HTH,

Deano


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: hxcpp enum bug

Jan_Flanders


On Thu, Jul 28, 2011 at 10:54 AM, Deano <[hidden email]> wrote:

Hi,

It seems that hxcpp generation has a bug when comparing enum values. If used directly in a ‘if’ it doesn’t happen, but use in a switch and it works.

I’m in the middle of something at the moment, so haven’t produces a minimal test case but my code is basically doing this.

enum ElementType {

                Single;

                Sett;

                Image;

                Page;

                Alias;

}

 

var eleType = Alias;

if( eleType == Alias ) trace(“w00t”);

switch(eleType ) {

   case Alias:

       trace( “yep” );

   default:

}

 

Console:

Yep


There is:

static function enumEq<T>( a : T, b : T ) : Bool

 in the Type class to compare two enum constructors/'values'
:
http://haxe.org/api/type

Jan

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: hxcpp enum bug

clemos
Hi,

This issue seems to happen quite often.
Maybe it would be worth raising a warning or error message when one
compares (==) two enums ?
Or maybe the == operator should be converted by the compiler to enumEq
when dealing with Enums ? I mean, does it ever make sense to compare
two Enums using ==, especially since the result seems to differ
between platforms / enum implementations ?

++++
Clément

On Thu, Jul 28, 2011 at 3:35 PM, Jan Flanders <[hidden email]> wrote:

>
>
> On Thu, Jul 28, 2011 at 10:54 AM, Deano <[hidden email]> wrote:
>>
>> Hi,
>>
>> It seems that hxcpp generation has a bug when comparing enum values. If
>> used directly in a ‘if’ it doesn’t happen, but use in a switch and it works.
>>
>> I’m in the middle of something at the moment, so haven’t produces a
>> minimal test case but my code is basically doing this.
>>
>> enum ElementType {
>>
>>                 Single;
>>
>>                 Sett;
>>
>>                 Image;
>>
>>                 Page;
>>
>>                 Alias;
>>
>> }
>>
>>
>>
>> …
>>
>> var eleType = Alias;
>>
>> if( eleType == Alias ) trace(“w00t”);
>>
>> switch(eleType ) {
>>
>>    case Alias:
>>
>>        trace( “yep” );
>>
>>    default:
>>
>> }
>>
>>
>>
>> Console:
>>
>> Yep
>
> There is:
>
> static function enumEq<T>( a : T, b : T ) : Bool
>
>  in the Type class to compare two enum constructors/'values':
> http://haxe.org/api/type
>
> Jan
>
> --
> haXe - an open source web programming language
> http://haxe.org
>

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: hxcpp enum bug

Gamehaxe
This seems like a good idea - may as well be an error.
In the case where you want to compare instance pointers, you
can cast to dynamic before you compare.

HUgh

> his issue seems to happen quite often.
> Maybe it would be worth raising a warning or error message when one
> compares (==) two enums ?
> Or maybe the == operator should be converted by the compiler to enumEq
> when dealing with Enums ? I mean, does it ever make sense to compare
> two Enums using ==, especially since the result seems to differ
> between platforms / enum implementations ?

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

RE: hxcpp enum bug

Deano
If comparing a var to an enum value is wrong, than a warning or an error
would be good. It does work on some platforms (Flash) and only bit me when
running it under hxcpp.
Ideally the compiler would issue warnings for anything that is
implementation dependent, perhaps have a pragma like system to switch them
off if you know and its okay? but telling you upfront you're in dodgy ground
would make portability easier.

Still not sure why its valid for a switch and not if's...

Thanks,
Deano

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Gamehaxe
Sent: 28 July 2011 15:44
To: The haXe compiler list
Subject: Re: [haXe] hxcpp enum bug

This seems like a good idea - may as well be an error.
In the case where you want to compare instance pointers, you
can cast to dynamic before you compare.

HUgh

> his issue seems to happen quite often.
> Maybe it would be worth raising a warning or error message when one
> compares (==) two enums ?
> Or maybe the == operator should be converted by the compiler to enumEq
> when dealing with Enums ? I mean, does it ever make sense to compare
> two Enums using ==, especially since the result seems to differ
> between platforms / enum implementations ?

--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: hxcpp enum bug

Julien CASTETS
Hi,

Did you tried fully qualified ?

if( eleType == ElementType.Alias ) trace(“w00t”);

I thought it would work like this, I remember facing the same issue, but I'm not sure since it was a while ago.

Hope it helps
Julien

2011/7/28 Deano <[hidden email]>
If comparing a var to an enum value is wrong, than a warning or an error
would be good. It does work on some platforms (Flash) and only bit me when
running it under hxcpp.
Ideally the compiler would issue warnings for anything that is
implementation dependent, perhaps have a pragma like system to switch them
off if you know and its okay? but telling you upfront you're in dodgy ground
would make portability easier.

Still not sure why its valid for a switch and not if's...

Thanks,
Deano

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Gamehaxe
Sent: 28 July 2011 15:44
To: The haXe compiler list
Subject: Re: [haXe] hxcpp enum bug

This seems like a good idea - may as well be an error.
In the case where you want to compare instance pointers, you
can cast to dynamic before you compare.

HUgh

> his issue seems to happen quite often.
> Maybe it would be worth raising a warning or error message when one
> compares (==) two enums ?
> Or maybe the == operator should be converted by the compiler to enumEq
> when dealing with Enums ? I mean, does it ever make sense to compare
> two Enums using ==, especially since the result seems to differ
> between platforms / enum implementations ?

--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: hxcpp enum bug

Nicolas Cannasse
In reply to this post by clemos
Le 28/07/2011 16:20, clemos a écrit :
> Hi,
>
> This issue seems to happen quite often.
> Maybe it would be worth raising a warning or error message when one
> compares (==) two enums ?

It is allowed to compared two enums without parameters, and the result
should be true if they are the same constructor.

The compiler already display an error when comparing with == an enum
with parameters, for instance :

enum E {
    A;
    B( v : Int);
}

trace(A == B(0)); // error

I guess there's an issue with hxcpp that needs to be fixed in order to
ensure that E.A == E.A.

Nicolas

--
haXe - an open source web programming language
http://haxe.org