groovy

Non commutative behavior of == operator with Gstring and Integer

Details

  • Type: Sub-task Sub-task
  • Status: Open Open
  • Priority: Major Major
  • Resolution: Unresolved
  • Affects Version/s: 1.1-rc-3
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None
  • Number of attachments :
    0

Description

int a = 41
int b = 4

void failsafe(Closure action) {
    try {
        action.call()
    } catch (Throwable e) {
        e.printStackTrace()
    }
}

GString gs = "${b}1"
failsafe { println gs == a } // true
failsafe { println a == gs } // throws ClassCastException

String s = '41'
failsafe { println a == s } // throws ClassCastException
failsafe { println s == a } // throws ClassCastException

I haven't attached a testcase as I'm not sure what the right behavior is.

Activity

Hide
blackdrag blackdrag added a comment -

it is a bug, but no critical bug. I set the priority to "Major" and the fix version to "1.2"

Show
blackdrag blackdrag added a comment - it is a bug, but no critical bug. I set the priority to "Major" and the fix version to "1.2"
Hide
Paul King added a comment -

It also raises the question about what should happen with integers and one-character GStrings:

def x = 65
def y = 'A'
def z = "${'A'}"
assert x == y  // OK
assert y == x  // OK
//assert x == z  // CCE
//assert z == x  // fails
assert y == z  // OK
assert z == y  // OK
Show
Paul King added a comment - It also raises the question about what should happen with integers and one-character GStrings:
def x = 65
def y = 'A'
def z = "${'A'}"
assert x == y  // OK
assert y == x  // OK
//assert x == z  // CCE
//assert z == x  // fails
assert y == z  // OK
assert z == y  // OK
Hide
blackdrag blackdrag added a comment -

I think we should fix compareTo so that it does not throw a CCE in any of these cases.

Show
blackdrag blackdrag added a comment - I think we should fix compareTo so that it does not throw a CCE in any of these cases.

People

Vote (0)
Watch (1)

Dates

  • Created:
    Updated: