Pinderkent

Pain and glory from the trenches of the IT world.

Groovy is clearly not a statically typed language.

Posted on Monday, February 18, 2008 at 12:40 PM.

I recently wrote about how undetected type errors when using languages like Ruby and Python can be quite dangerous. That article has gotten some notice, with one response considering Groovy.

The article about Groovy purports to have an example of a statically typed Groovy script that compiles fine, but results in a type error at runtime. The script is:

int x = "test"
The error received at runtime is:
Caught: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot
cast object 'test' with class 'java.lang.String' to class 'java.lang.Integer'
at typesafe.run(typesafe.groovy:1)
at typesafe.main(typesafe.groovy)

If Groovy were truly a statically typed language, then the compiler would have raised a compile-time error. But since that did not happen, one or more of the following may be true:

  1. The Groovy compiler does not perform adequate compile-time typechecking.
  2. The Groovy compiler performs compile-time typechecking, but a bug prevents it from catching this particular error.
  3. The semantics of Groovy prevent it from being a statically typed language.

The essence of static typing is that such an error would always be caught at compile time. We can't say that a language is statically typed if such an error occurs at runtime. Just because the programmer can specify the type of a variable (eg. 'int' in this case), it does not mean that the language is statically typed. This is especially true if the compiler or interpreter completely ignores such type specifiers at compile-time.

So in this case, I think the verdict is quite clear: Groovy cannot be considered a statically typed language, as it exhibited a runtime type-related error.

Permalink: http://pinderkent.phumblog.com/post/2008/02/groovy_is_clearly_not_a_statically_typed_language
Share:
Feeds
  • RSS 2.0 Feed
  • Atom 2.0 Feed
Tags
Archives