Gangmax Blog

自由之思想,独立之精神

A Problem of GStringImpl, Groovy or Java?

| Comments

When I was using Groovy to compare two Strings like below, I got a “false” result which was expected to be “true”:

1
2
3
if(foo.equals("some value")) {
...
}

It took me some time to realize that the problem is: the “foo” variable is not a String instance, but a GStringImpl instance instead. So the official Groovy code here should be:

1
2
3
if(foo == "some value") {
...
}

You can get more details about this problem from here and here.

Problem resolved? No! The real problem here is: this is unintuitive, “GStringImpl” is not “String”! But why?

I can still remember Neal Ford even said, several years ago, which really impressed me:

… That’s crazy because Java is an object-oriented language and the way you interoperate and work in an object-oriented language is you sub-class things, but the fundamental types, like string, you are not allowed to sub-class, they made it final. I don’t know why. …

This fact also reminds me that, even a dynamic type language based on JVM cool as Groovy, the things it get from JVM are not all cool. And pathetically, the Groovy language can do nothing with it.

Comments