Every now and then somebody writes that Groovy supports optional static typing. But it doesn't. They've confused the use of type annotations on variables with a static type system.
This article is emphatically not an entry in the long, sad, and mostly insipid static vs. dynamic debate. It is a clarification of a misunderstanding.
Definitions
The word "static" in "static type system" is related to phrases like "static analysis." Basically, without executing the code in question, a static type system analyzes code to prove that certain kinds of misbehaviors won't ever happen.
The word "dynamic" in "dynamic type system" is related to the "dynamic execution" of a program. In contrast to a static type system, a "dynamic type system" automatically tests properties when expressions are being (dynamically) evaluated.[1]
The Proof
With those definitions its easy to show that adding type annotations to a Groovy program does not make it statically typed. Create a file named test.groovy. In it define a couple of unrelated classes
class Foo {} class Bar {}
Now write a function that promises to take a Bar and return a Foo. Add a println for fun.
Foo test(Bar x) { return x } println("Everything's groovy")
Compile it and run it (explicitly compiling isn't necessary, it just reinforces my point)
~/test$ groovyc test.groovy ~/test$ groovy test Everything's groovy
There were no complaints at all. A static type system would have rejected the program.
Open up test.groovy and make a slight adjustment so that the function is actually called
Foo test(Bar x) {return x} println("Everything's groovy") test(new Bar())
Compile and run this
~/test$ groovyc test.groovy ~/test$ groovy test Everything's groovy Caught: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'Bar@861f24' with class 'Bar' to class 'Foo' at test.test(test.groovy:4) at test.run(test.groovy:6) at test.main(test.groovy)
A runtime exception is finally thrown at the (dynamic) moment when the test function is executed. That's dynamic checking rather than any form of static proof.
Actual Static Typing
Now open a file named test.scala.
class Foo class Bar def test(x : Bar) : Foo = x
Compile as a script
~/test$ scala -Xscript test test.scala (virtual file):1: error: type mismatch; found : this.Bar required: this.Foo object test { ^ one error found
Ta da, static typing. With no attempt to execute my test function the type checker says "I have reason to believe this might go sideways."
The Misunderstanding
There's an oft repeated phrase that "in a statically typed language variables are typed." There's also a common misunderstanding that static typing and type annotations are the same thing. I can show a simple counter example to both misconceptions with one language: Haskell.
Create a file named Test.hs. Here's a function called flatten. It flattens a list of lists one "level" to just be a list.[2]
flatten = foldl (++) []
No variables were harmed during the creation of that function. It's written in what's called "point free" style. Yet with neither variables nor type annotations I can show it's statically typed by defining a bogus main function
main = print $ flatten [1,2,3,4,5,6]
And trying to compile
~/test$ ghc Test.hs -o test Test.hs:3:24: No instance for (Num [a]) arising from the literal `1' at Test.hs:3:24 Possible fix: add an instance declaration for (Num [a]) In the expression: 1 In the first argument of `flatten', namely `[1, 2, 3, 4, ....]' In the second argument of `($)', namely `flatten [1, 2, 3, 4, ....]'
Thus you need neither variables nor annotations to have static typing. A small change fixes things right up
main = print $ flatten [[1,2,3],[4,5,6]] ~/test$ ghc Test.hs -o test ~/test$ ./test [1,2,3,4,5,6]
It's easy enough to see what type GHC has assigned the function. Just add a module declaration at the very top of the file
module Main (flatten, main) where
And fire up ghci
~/test$ ghci Loading package base ... linking ... done. Prelude> :load Test Ok, modules loaded: Main. Prelude Main> :type flatten flatten :: [[a]] -> [a]
Exactly as described, it takes a list of lists and returns a list.
Groovy Does Have Static Typing
Having shown that Groovy does not have static typing let me show that it does have static typing. :-) At least, it has static typing for some kinds of things. Open up test.groovy again and add this
class MyRunnable implements Runnable {}
Compile that and you get an error
~/test$ groovyc test.groovy org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed, test.groovy: 8: Can't have an abstract method in a non-abstract class. The class 'MyRunnable' must be declared abstract or the method 'void run()' must be implemented. @ line 8, column 1. class MyRunnable implements Runnable {} ^ 1 error
The error happens without executing anything so it's a form of static analysis, and it's caused by failure to adhere to the Runnable type's requirements hence it's a static type error. But it's not an optional static type check - you can't turn it off.
Interesting, no?
Conclusion
Whatever you feel positively or negatively about Groovy please don't perpetuate the myth that it has optional static types. It doesn't. Groovy's optional type annotations are used for dynamic testing not static proofs. It's just that with type annotations Groovy dynamically tests are for "dynamic type" tags attached to objects rather than its more normal mechanism of dynamically testing for the presence of certain methods. What little static typing Groovy does have is not optional.
Update: Some time after this article was written, a team announced Groovy++, an extension to Groovy that really does have optional static typing.
Footnotes
[1] In a certain formal sense the phrase "dynamic type system" is an oxymoron and "static type system" is redundant. However, the phrases are commonly used and there doesn't seem to be a commonly recognized concise phrase to describe what a "dynamic type system" does. Pedants will argue for "untyped" but that doesn't give me a concept on which to hang the different ways that say Groovy, Ruby, and Scheme automatically deal with runtime properties.
[2] A more general function called join already exist for all monads in Control.Monad module. It can be defined as "join m = m >>= id"