5

Following is 2 lines of code:

def name = "Neo"
println name

If I execute it in groovysh, I will get Unknown property: name error. If I execute it in groovyConsole, everything goes on well.

Neo
  • 2,196
  • 5
  • 32
  • 58
  • 1
    @dmahapatro: i think [this question](http://stackoverflow.com/q/7130529/217324) is a better candidate for closing as a duplicate than that one (which also has the Set declaration issue going on). but i think the question is less 'why does groovysh do this' than 'why was groovysh designed that way'. which may not be answerable, this may just be quibbling over the close reason. – Nathan Hughes Jan 23 '15 at 18:35
  • 1
    @NathanHughes one of the answer ( from @dmahapatro ) has comments about the reason why `groovysh` behaves as such. But you are correct the other question would be a better duplicate. However, this has been fixed in Groovy 2.4.0 as stated in the answer below. – dmahapatro Jan 23 '15 at 18:43

1 Answers1

6

If you want features from Groovy 2.4.0 you can use

:set interpreterMode true to see a difference. :)

groovy:000> def a = 10
===> 10
groovy:000> a
Unknown property: a
groovy:000> :set interpreterMode true
groovy:000> a
Unknown property: a
groovy:000> def b = 100
===> 100
groovy:000> b
===> 100
groovy:000>
dmahapatro
  • 49,365
  • 7
  • 88
  • 117
  • Slap this into your ~/.groovy/groovysh.rc file if you want to make it the default behavior. – Apteryx Jun 20 '18 at 21:01
  • @Apteryx Actually for me it sets seem to persist across sessions--although I hate to rely on that. – Bill K Oct 18 '18 at 19:59