Check if variable is Int32

86 views
Skip to first unread message

Daniel Kuschny

unread,
Jan 19, 2013, 12:13:45 PM1/19/13
to haxe...@googlegroups.com
Hi everybody. 

The Int32 Type seems to be implemented in a very special way: There are no Int32 instances, they directly handled by the target platform. For some reason I need to check whether a variable is an Int32 instance but the usual Std.is(value, Int32) fails in this case (e.g. compiling to Neko). 

Is there a way to check if a variable is an Int32 instance?

public static function isInt32(o:Dynamic) : Bool
{
   
...
}

isInt32
(10) // false
isInt32
(Int32.ofInt(10)) // true

Cheers
Daniel

Marc Weber

unread,
Jan 19, 2013, 12:43:33 PM1/19/13
to haxelang
Excerpts from Daniel Kuschny's message of Sat Jan 19 18:13:45 +0100 2013:
> Is there a way to check if a variable is an Int32 instance?
There will never be because eg javascript only knowns about floats.

So for runtime values there is no chance. That's why I recommend talking
about the exact case.


Marc Weber

Daniel Kuschny

unread,
Jan 19, 2013, 12:54:51 PM1/19/13
to haxe...@googlegroups.com
For a project I need a HashMap and HashSet and therefore I created a HashCode generation algorithm which computes HashCodes of any object. The only problem is the Int32 type: In my neko unit tests the Int32 type cannot be recognized as Int32 which leads to an invalid HashCode. It would be great if the Int32 class would work like the Int64 class which has a central implementation across all plattforms.

Luca

unread,
Jan 19, 2013, 1:02:49 PM1/19/13
to haxe...@googlegroups.com
Well Int32 is only necessary on neko... and with haxe 3.0 this will change with new version of neko that uses 32bit ints natively and Int32 will no longer be needed at all.

(Hope I got nothing wrong there)

Juraj Kirchheim

unread,
Jan 20, 2013, 9:09:22 AM1/20/13
to haxe...@googlegroups.com
On Sat, Jan 19, 2013 at 6:54 PM, Daniel Kuschny <danie...@gmail.com> wrote:
For a project I need a HashMap and HashSet and therefore I created a HashCode generation algorithm which computes HashCodes of any object. The only problem is the Int32 type: In my neko unit tests the Int32 type cannot be recognized as Int32 which leads to an invalid HashCode. It would be great if the Int32 class would work like the Int64 class which has a central implementation across all plattforms.

Two remarks: 

- On neko I would use $hkey for hashing: http://nekovm.org/doc/view/builtins
- To my understanding, an Int32 is just an Int at runtime. There's no real harm treating them the same, at least as long as you're type safe. For a given key type, every subfield has a welldefined type and can only either be Int32 or Int. A collision is only possible if something is untyped and you actually have two objects that are structurally equal at runtime, except for the fact that one Int is meant to represent an Int and the other one an Int32 respectively. If that's your biggest worry, then your hashing algorithm must be really good ;)

Regards,
Juraj

Daniel Kuschny

unread,
Jan 21, 2013, 9:11:39 AM1/21/13
to haxe...@googlegroups.com
This way the hashing wouldn't be equal across plattforms. Of course I could write a wrapper which uses platform specific features for implementing the HashMap but by implementing it completely in Haxe I can guarantee the same behavior everywhere. 

I decided to simply ignore the Int32 type and the Neko platform for now. 
Reply all
Reply to author
Forward
0 new messages