Type incoherence when compiling Java code through GWT

29 Views Asked by At

I am building a web project using the GWT library. For some reason when compiling through GWT pieces of code similar to this

double a = 0, b = 0;
long c = Double.doubleToLongBits(Double.sum(a, b));

It gives me the following error: The method sum(double, double) is undefined for the type Double.

If I write the same exact code in a normal Java 8 project it gives me no trouble. I have a feeling the GWT library has some limitations when translating from Java to JavaScript but I found no way to confirm this through the documentation.

1

There are 1 best solutions below

0
Colin Alworth On

GWT's JRE emulation is somewhat limited, there are a few categories of things that are not supported. These generally are related to IO, reflection, and threading, but numerics can cause some limitations too. For example, emulating a Java float fully would be expensive (JS's Number type is effectively a Java Double, even for int operations), so some operations might be more precise than expected.

The GWT website includes a listing of all currently supported JRE emulation at https://www.gwtproject.org/doc/latest/RefJreEmulation.html. This is for the latest version - GWT makes a point to not break backwards compatibility until confident that it won't matter (e.g. the demise of IE) or where there is no other choice due to dependency upgrades (GWT 2.12 will drop support for Java 8 so that Java 17 sources can be compiled).

The commit that added Double.sum(double, double) is https://github.com/gwtproject/gwt/commit/ac5d74820b313d17fc0bcab902195afba7f5c59c, and was for GWT 2.8, the first release that could compile Java 8 sources. This makes sense, since that method was first added to the JRE in Java 8, see https://docs.oracle.com/javase/8/docs/api/java/lang/Double.html#sum-double-double-

Since:

1.8

GWT 2.7 is too old to expect any Java 8 features to work on it. Even GWT 2.8 is over 8 years old at this point...