tags:

views:

337

answers:

4

Hi,

In Groovy types are optional so you can use either:

String foo = "foo"
foo.noSuchMethod()

or

def foo = "foo"
foo.noSuchMethod()

I assumed that the first example would generate a compile-time error, whereas the second would only fail at runtime. However, this doesn't appear to be the case. In my experience, a compile-time error is generated in neither case.

Am I right in assuming then that the only benefit of declaring the type of a reference is as a form of documentation, i.e. to communicate intentions to other programmers. For example, if I write a method such as:

def capitalize(String arg) {
    return arg.toUpperCase()
}

This communicates the type of arguments that should be passed to the function much more effectively than:

def capitalize(def arg) {
    return arg.toUpperCase()
}

Does the Groovy compiler perform any type-checking when types are specified?

Thanks, Don

+2  A: 

In Groovy, type checking is done dynamically at runtime. The benefits of variables with type is that you can be sure that it contains the value you expect them to have, otherwise you get a runtime exception that you can catch and do whatever you need to do to handle the exception.

Cesar
+2  A: 

As Cesar said, type checking is a run-time process, one of the major reasons that Groovy is slower than Java (not that that's really bad).

You can see why this is, right? Given the dynamic nature of Groovy, it's near-impossible to tell if String has been extended somewhere else in your code to contain a method noSuchMethod(). The same goes for member type-checking, as it's entirely possible to remove a member of one type, and add a member of another type with the same name later in code. It's probably not common, but very possible.

The question is, how much type checking do you really need? You're calling the method, you really should know what arguments it takes, or if the method actually exists. Using compile-time checking to save you the time of looking it up isn't a core usefulness of the compiler.

Bill James
The new InvokeDynamic in JDK 7 will improve the speed of Groovy. Most of the performance issues with the dynamic languages on the JVM have to do with the limitations that the VM imposes on the language. Most of the things behind the scenes with Java are dynamic, it is just the language that imposes the static types.
Joshua
Yes, and I wasn't complaining about the speed, just explaining.
Bill James
who says its slower? (curious)
Blankman
+1  A: 

Compile time-checking in Groovy is close to impossible for types. Your example of

String foo = "foo"
foo.noSuchMethod()

Would work beautifully given that previously an instruction was executed along the lines of

String.metaClass.noSuchMethod { -> println "Yes there is such a method"}
Robert Munteanu
It's also difficult for signature checking. About the only thing you can get is key-word spelling and brace-matching.
Bill James
A: 

One reason you might specify a type is to allow an IDE to help you.

def foo
foo.[ctrl-space]

... won't help you very much

List foo
foo.[ctrl-space]

... might (depending on the IDE) give you a choice of List's methods. Of course, a method that's not one of the choices might be a valid one to use, for the reasons given in other answers.

There are other automated software maintenance operations that benefit from clues about types. For example, refactoring.

slim