cincheo / jsweet

A Java to JavaScript transpiler.
http://www.jsweet.org
Other
1.46k stars 160 forks source link

Transpile String Equals #555

Closed wj1918 closed 4 years ago

wj1918 commented 4 years ago

Strict comparison should be used for same type comparison.

For example, the following java code

"Hello".equals("World");

should convert to

"Hello"==="World"

current output is

/* equals */ (function (o1, o2) { if (o1 && o1.equals) {
    return o1.equals(o2);
}
else {
    return o1 === o2;
} })("Hello", "World");

Further more, use == for different type comparison.

According to the MDN document

JavaScript has both strict and type–converting comparisons. A strict comparison (e.g., ===) is only true if the operands are of the same type and the contents match. The more commonly-used abstract comparison (e.g. ==) converts the operands to the same type before making the comparison

lgrignon commented 4 years ago

Hello @wj1918, First of all, thanks for your feedback.

They've been some discussions about how should be transpiled hashCode and equals. There is no unique solution given that JS and Java equality mechanism are pretty different.

The current compromise (the output you posted) is IMHO a good way of providing compatibility with existing Java code and at the same time being easy to understand.

There won't be any more modifications of this behavior for now.