Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Warning
titleGStrings aren't Strings

GString and String are two distinct classes, and hence use of GString objects as keys for Map objects or comparisons involving GString objects, can produce unexpected results when combined with String objects since a GString and a String won't have the same hashCode nor will they be equal. There is no automatic coercion between the two types for comparisons or map keys, so it's sometimes necessary to explicitly invoke toString() on GString objects.

On the other hand GStrings can easily be made into Strings

Code Block
def s = "String "
def g = "GString created at ${new Date()}"
def x = s + g
assert s instanceof String
assert g instanceof GString
assert x instanceof String

Unexpected conversion to String can lead to problems when code is expecting a GString, as for methods in groovy.sql classes. That's a case were explicit types can be benefifialbeneficial:

Code Block
def s = "String "
def g = "GString created at ${new Date()}"
GString x = GString.EMPTY + s + g
assert s instanceof String
assert g instanceof GString
assert x instanceof GString
println x

...