Jochen Theodorou to Anton Shepelev:

> > String test
> >
> > void func()
> > {
> >    println(test)
> > }
> >
> > test = 'hello'
> > func()
> >
> > Why, then, is `test' inaccessible from the function in
> > my script?
>
> because the method you defined is not nested in another
> method, instead it exists parallel to the other method
> that represents your script body, and they do not share
> local variables.

Thanks for the explanation, Jochen.

> Frankly... for years we have been defending this position,
> but now, with so much distance I actually really wonder
> why we keep this.

Perhaps it would have helped if you had documented not only
language features but also their rationale (justification),
at least is difficult, debatable cases.

> It comes up as a problem on a regular base, but wouldn't
> be so hard to "fix" really. I mean it is not broken, it
> works as designed, just not very intuitive.

Now that I know the answer, I am not sure the behavior
should be changed: the must be a way to dictinguish between
local variables of the `run' method, the class fields, and
the binding variables.

> it is actually less the assignment operator itself, it is
> the effect of scopes of dynamic variables. To compare.. in
> Java you have a static scope for a variable.

But I do not know Java and am learning Groovy as a stand-
alone language, not as a Java derivative.

> The compiler knows at any time where a variable is
> declared, and where to write it to (local variable or
> class). In Groovy we also have the static variable scopes,
> but they are "extended" in dynamic Groovy with dynamic
> variables. This means the (meta) class is "asked" for the
> value and existence of a variable at runtime as soon as we
> leave the static scope.

Somewhat similar to metatables in Lua, isn't it?  Why does
not the same thing work from class methods, even from static
class methods? --

   class Boo {
       static def boo()
       {    x = 1
       }
   }

> I would have loved to point to our documentation for this,
> but I actually am unable to find this there.

In another reply, Maarten Boekhold refers to section 3.4
(Variables) of the "Program structure" document, which has
the following to say about assignments to undeclared (or
undefined?) variables:

   if the variable is undeclared, it goes into the script
   binding. The binding is visible from the methods, and is
   especially important if you use a script to interact with
   an application and need to share data between the script
   and the application.

The explanation is OK, but I failed to look into that
document. Do you think you can provide better documentation
at a better place? If so, I should certainly appreciate it.
I also thank Maarten for reply.

Reply via email to