As the screenshot shows, While I'm comparing the same literal values with type of Float and Long, Scala says it's not equal
But the evaluator says they are equal to each other!
I suspect this is a bug in Intellij Idea, since the same code will yield different results in a normal Scala runtime than in a evaluator runtime.
If the Evaluator's result is untrustworthy, this may cause trouble for developers.
I wonder if there is anything wrong with my thinking and hope someone can point it out.
CodePudding user response:
I could not reproduce your example using the Intellij evaluator (though I don't use a Mac, this might be a bug on the Mac version of Intellij), but what I can say is that Java and Scala treat this differently. Actually, in Scala 2.13.x those values are equal.
Java treats this code differently based on their types (primitive vs wrapper types):
long vl = 32294629407L;
float vf = 32294629407f;
Boolean res11 = vl == vf;
System.out.println(res11); // true
Long vl1 = 32294629407L;
Float vf1 = 32294629407f;
Boolean res22 = vl1.equals(vf1);
System.out.println(res22); // false
Since Scala was designed to be a more regular language, such discrepancies were removed from the language, by special casing the ==
method for these classes:
val vl: Long = 32294629407L
val vf: Float = 32294629407f
val res11 = vl == vf // true
println(res11) // true
The only case where == does not directly call
equals
is for Java's boxed numeric types.
NOTE:
For prior versions of Scala, this does not hold, as it seems in Scala 2.12 this is not valid.