Details
-
Type:
Bug
-
Status:
Closed
-
Priority:
Minor
-
Resolution: Fixed
-
Affects Version/s: None
-
Fix Version/s: 1.1-beta-2
-
Component/s: groovy-jdk
-
Labels:None
-
Number of attachments :
Description
BigDecimals carry both a numerical value and a precision (scale).
In Groovy, when BigDecimals are compared, only the numerical value is taken into account, but isCase() uses both value and scale.
This should be consistent.
assert 0.5 == 0.50000 // works
switch(0.5) {
case 0.50000: println "match" // doesn't match
}
It looks like a simple method in DefaultGroovyMethods.java is all that is needed:
public static boolean isCase(Number caseValue, Number switchValue) {
return NumberMath.compareTo(caseValue, switchValue)==0;
}
The attached patch contains this method and adds some tests to SwitchWithDifferentTypesTest.groovy.