Google Web Toolkit’s (GWT) Fake Long

Google Web ToolkitI ran into a cunning Google Web Toolkit JavaScript problem, related to the Java long data type. I was using long for my objects’ identities, that are persisted to db4o. On the server side the objects’ identities had the correct values. When I returned the object as a serialized XML string, the value was also correct. But as soon as I tried to directly use long, the value was always one less, than the actual value it was meant to be.

Before I decided on long for my object identities, I had a gut feeling that JavaScript might not support this data type. Turns out my original suspicion was valid: “Intrinsic types byte, char, short, int, long, float, double, Object, String, and
arrays are supported. However, there is no 64-bit integral type in JavaScript, so variables of type long are mapped onto JavaScript double-precision floating point values. To ensure maximum consistency between hosted mode and web mode, we recommend that you use int variables.”

It probably would be a good idea if the GWT compiler reported any long types as warnings. There’s no point having a long that doesn’t really work. It just opens the opportunity for problems like this one.

About these ads

4 Comments on “Google Web Toolkit’s (GWT) Fake Long”

  1. Any chance you can contribute your app (or a sample) to projects.db4o.com?

    Best!

    German

  2. openlandscape says:

    This is definitely planned. I’d like to flesh the project out a little bit more, before making it public. But I promise as soon as some key features have been completed, GhostBlade will be at projects.db4o.com!

  3. German says:

    Nice thanks! You can reach me at german -AT – db4o.com
    Best regards!!
    German

  4. Dave Watson says:

    Just in case anybody sees this post (since it shows up highly ranked in Google), this is NO LONGER TRUE as of GWT 1.5 and higher:

    http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsCompatibility.html

    Now long is emulated via 2 32-bit values. It may be slow if you’re using it a lot, but you won’t get off-by-one errors.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.