1

I am aware that a variable can be dynamically typed with the def keyword in Groovy. But I have also noticed that in some circumstances it can be left out, such as when defining method parameters, eg func(p1, p2) instead of func(def p1, def p2). The latter form is discouraged.

I have noticed that this is extendable to all code - anytime you want to define a variable and set its value, eg var = 2 the def keyword can be safely left out. It only appears to be required if not instantiating the variable on creation, ie. def var1 so that it can be instantiated as a NullObject.

Is this the only time def is useful? Can it be safely left out in all other declarations, for example, of classes and methods?

Psymøn
  • 413
  • 2
  • 4
  • 15

2 Answers2

4

Short answer: you can't. There are some use cases where skipping the type declaration (or def keyword) works, but it is not a general rule. For instance, Groovy scripts allow you to use variables without specific type declaration, e.g.

x = 10

However, it works because groovy.lang.Script class implements getProperty and setProperty methods that get triggered when you access a missing property. In this case, such a variable is promoted to be a global binding, not a local variable. If you try to do the same on any other class that does not implement those methods, you will end up getting groovy.lang.MissingPropertyException.

Skipping types in a method declaration is supported, both in dynamically compiled and statically compiled Groovy. But is it useful? It depends. In most cases, it's much better to declare the type for a better readability and documentation purpose. I would not recommend doing it in the public API - the user of your API will see Object type, while you may expect some specific type. It shows that this may work if your intention is to receive any object, no matter what is its specific type. (E.g. a method like dump(obj) could work like that.)

And last but not least, there is a way to skip type declaration in any context. You can use a final keyword for that.

class Foo {
    final id = 1

    void bar(final name) {
        final greet = "Hello, "

        println greet + name + "!"
    }
}

This way you can get a code that compiles with dynamic compilation, as well as with static compilation enabled. Of course, using final keyword prevents you from re-assigning the variable, but for the compiler, this is enough information to infer the proper type.

For more information, you can check a similar question that was asked on SO some time ago: Groovy: "def" keyword vs concrete type

Szymon Stepniak
  • 40,216
  • 10
  • 104
  • 131
1

in Groovy it plays an important role in Global and Local variable

if the variable name is same with and without def

def is considered local and without def its global

I have explained here in detail https://stackoverflow.com/a/45994227/2986279

So if someone use with and without it will make a difference and can change things.

Gaurav Khurana
  • 3,423
  • 2
  • 29
  • 38