I've recently been reading about the behaviour of GStringImpl
s vs String
s when used in collections in Groovy.
I understand that the reason this evaluates to false...
"${'test'}".equals("test") == false
is due to the symmetry requirement of the .equals()
contract, however I was wondering if there was a reason the GStringImpl
couldn't just be evaluated to a String
immediately. So when I do something like this...
"${'someString'}"
I don't get a GStringImpl
, I just get a plain Java String
back, which I can immediately use as the key in a map, for example.
I know there are some workarounds, like
String s = "${'someString'}"
however stuff like this is a bit inconvenient, and the mixup between GStringImpl
and String
seems to be a big 'gotcha' for Groovy newbees.
GStrings are not evaluated inmediately to String because of some reasons, mainly related to lazy evaluation (which is quite good for logging) and templating. In Strings and GString you can find a good explanation:
Therefore: