39

In the Groovy shell you can type in commands, such as

def x = 1

and run them. That line comes back with:

groovy:000> > def x = 1
def x = 1
===> 1
groovy:000>

Now if I type:

 println(x) 

I get:

groovy:000> > println(x)
println(x)
Unknown property: x
groovy:000> 

So it seems that the console nor shell remembers object definitions, is this normal?

2 Answers 2

61

This is standard behavior in the Groovy shell, not peculiar to the Grails shell. You probably don't want to def the variable. See the following:

~ $ groovysh
Groovy Shell (2.3.4, JVM: 1.7.0_45)
Type ':help' or ':h' for help.
-------------------------------------------------------------------------------
groovy:000> def x = 42
===> 42
groovy:000> x
Unknown property: x
groovy:000> y = 2112
===> 2112
groovy:000> y
===> 2112
groovy:000>

From http://beta.groovy-lang.org/groovysh.html

1.3.4. Variables

Shell variables are all untyped (ie. no def or other type information).

This will set a shell variable:

foo = "bar"

But, this will evaluate a local variable and will not be saved to the shell’s environment:

def foo = "bar"

You can change this behaviour by enabling interpreterMode

groovy:000> := interpreterMode
groovy:000> def x = 42
===> 42
groovy:000> x
===> 42
groovy:000>
6
  • 12
    Wow, this is really confusing. Why would declaring the type in standard java way, or using def in the grails way, fail in the console, but not in the app? I have spent an hour searching for examples showing how to use the console, including going through my 6 groovy and gails books, with not a single entry. How did you work this one out? Nov 3, 2014 at 15:21
  • 2
    "How did you work this one out?" - I worked it out because I am on the Groovy and Grails team and am familiar with how this works. Nov 3, 2014 at 15:22
  • I realize that link might not be a good link in the future when folks find this answer so I should include more info here. The docs linked above currently says the following about def'd variables... "...this will evaluate a local variable and will not be saved to the shell’s environment." Nov 3, 2014 at 15:26
  • That is a great link! it has everything I was looking for - nice one. Nov 3, 2014 at 15:36
  • 1
    := interpreterMode fixes the problem perfectly!! Thank you!!! May 14, 2020 at 16:29
0

"def" are more like compiled variables in Java way (to some degree), compiled (maybe type is unknown/dynamic, but name/existence of variable / property is known).

def xyz = 1 -> Object xyz = 1;

Without "def" are added to specific container Binder by name, in fully dynamic manner. Imagine this like specific Map (sorry for simplicity)

binder["xyz"] = 1;

My personal filling is that Groovy doc don't illustrate this (huge) difference clearly. Word "untyped" seems to weak. Implementation of "storage" for this two is totally different.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.