groovy

as Integer doesn't work with GStringImpl

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Fixed
  • Affects Version/s: 1.6.5
  • Fix Version/s: 1.6.6, 1.7-rc-1
  • Component/s: groovy-jdk
  • Labels:
    None
  • Number of attachments :
    0

Description

"-1" as Integer

returns -1

however:

a = '-1'
"$a" as Integer

gives

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '-1' with class 'org.codehaus.groovy.runtime.GStringImpl' to class 'java.lang.Integer'
    	at Script1.run(Script1.groovy:2)

But you can do it with:

a = '-1'
( "$a" as String ) as Integer

Tim

Activity

Hide
Roshan Dawrani added a comment -

Fixed

Show
Roshan Dawrani added a comment - Fixed
Hide
blackdrag blackdrag added a comment -

Roshan, your fix changes DefaultTypeTransformation which is used for casts, that means due to your change (Integer) "$a" will now behave as "$a" as Integer, but (Integer) "-1" is not supposed to return -1, while "-1" as Integer is. the as-operation is not a normal cast, instead it is mapped to asType. Which means any change to fix GString.asType would be in DefaultGroovyMethods and not in DefaultTypeTransformation

Show
blackdrag blackdrag added a comment - Roshan, your fix changes DefaultTypeTransformation which is used for casts, that means due to your change (Integer) "$a" will now behave as "$a" as Integer, but (Integer) "-1" is not supposed to return -1, while "-1" as Integer is. the as-operation is not a normal cast, instead it is mapped to asType. Which means any change to fix GString.asType would be in DefaultGroovyMethods and not in DefaultTypeTransformation
Hide
Roshan Dawrani added a comment -

Sorry about that. I didn't know that behavior difference in '(Integer) "$a"' and '"$a" as Integer'. And no test failed too to tell me that I was breaking this normal cast behavior.

I have fixed it now.

Can you please tell me why '(Integer) "$a"' and '"$a" as Integer' are supposed to produce different results? I thought it was just a matter of coding style. Do these different styles yield different results often?

Show
Roshan Dawrani added a comment - Sorry about that. I didn't know that behavior difference in '(Integer) "$a"' and '"$a" as Integer'. And no test failed too to tell me that I was breaking this normal cast behavior. I have fixed it now. Can you please tell me why '(Integer) "$a"' and '"$a" as Integer' are supposed to produce different results? I thought it was just a matter of coding style. Do these different styles yield different results often?
Hide
Paul King added a comment -

Casting we try to keep close to Java semantics. "as" gives us an extra hook to do some extra Groovy coercing of like things.

Show
Paul King added a comment - Casting we try to keep close to Java semantics. "as" gives us an extra hook to do some extra Groovy coercing of like things.
Hide
Roshan Dawrani added a comment -

Didn't know that groovy applied different degrees of coercion in ()/as syntaxes. Thanks.

Show
Roshan Dawrani added a comment - Didn't know that groovy applied different degrees of coercion in ()/as syntaxes. Thanks.

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: