Google Web Toolkit’s (GWT) Fake Long
Posted: February 24, 2008 Filed under: Java, Web | Tags: GoogleWebToolkit, Java, JavaScript, Long 4 Comments »
I 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.










Any chance you can contribute your app (or a sample) to projects.db4o.com?
Best!
German
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!
Nice thanks! You can reach me at german -AT – db4o.com
Best regards!!
German
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.