groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Assia Alexandrova <kob...@gmail.com>
Subject Re: JSR 223 support behavior
Date Wed, 24 May 2017 03:11:59 GMT
Hi,

On Tue, May 23, 2017 at 6:39 AM, Jochen Theodorou <blackdrag@gmx.org> wrote:
>
>
> On 23.05.2017 03:34, Assia Alexandrova wrote:
>>
>> Hi,
>>
>> Thanks for the prompt response!
>>
>> I am a bit confused. According to what appears to be the official
>> language semantics:
>>
>> http://www.groovy-lang.org/semantics.html#_variable_definition
>>
>> the 'def' keyword is used to declare an untyped variable (i.e. one
>> whose type is Object). So something like
>>
>> def x = 10
>>
>> is then presumably equivalent to two separate statements:
>>
>> def x
>> x = 10
>>
>> What is the difference in semantics between 'def x = 10' and 'x = 10'?
>
>
> "def x" declares a variable x in the current lexical scope.
>
> "x=10" is an assignment to the variable x. In Java the assignment has to be
> done either together with the declaration or after (in a lexical sense) the
> declaration. While Groovy has lexical scopes, Groovy also has some
> constructs with dynamic scopes. Every Closure and every class container can
> define such a dynamic scope. A script is also a class container. The
> important point here is, that in a dynamic scope the declaration may not be
> literal, but programmatically.
>
> So if you do println x, as script, then what are the scopes here? The
> println method call actually is taken and put into a run method, which also
> defines the scope. So the direct scope context for x is the same as the
> method... besides the implicit method, nothing different to Java here yet.
> The parent to the method scope is the class scope. In a script you cannot
> easily define elements here. Unlike Java Groovy does not define this scope
> as lexical, it defines it as dynamic. This means getProperty and setProperty
> methods are used to get or set the value of "x". Thus x is not bound to a
> declaration anymore.
>
> In a script these getProperty and setProperty methods will use the Binding
> to get or set such a value. With the logic of "I can always set the value"
> and "I can get the value only if it has been set before".

Ok, so if I understand correctly from this and from some cursory
reading of the source code around JSR 223, the 'def' keyword operates
within a lexical scope and that lexical scope must be completely known
in advance before the statement can be evaluated. There is no "global"
lexical scope (just like there isn't in Java) that one can operate
within in a REPL. However, there is a global dynamic scope that is
accessible by being careful to omit the 'def' keyword (and I suppose
type names as well?). I must admit I don't understand what the
semantics are, but as long as it makes some sense to Groovy users,
that's fine.

>> Can you point me to how docs where I can learn about transforms and
>> write such a transform?
>
>
> Take a look at
> http://groovy-lang.org/metaprogramming.html#developing-ast-xforms

Great, I will! Looks interesting...

>> I'd like to understand if this behaviors is
>> some side-effect of the implementation of the language or really
>> designed following some rationale. In the latter case, cool, but in
>> the former, I'd take a stab at a writing a transform.
>
>
> well, I hope my explanation above sounds like the cool case ;)
>
>> The following REPL interaction seems too strange to me :-)
>>
>> groovy:000> def x = 10;
>>
>> ===> 10
>>
>> groovy:000> x
>>
>> Unknown property: x
>
>
> hmmm... strange... I have the vague memory that somebody already fixed
> that... ah... right... this is JSR-223 not groovysh. For groovysh we
> actually do quite some things like transferring import statements and I
> think also declaration of variables and methods. But JSR-223 was originally
> intended to always get a complete script, thus we have no such things there.
> If JSR223 would be in more wider use, we would probably offer a REPL mode
> for it...

This *is* groovysh, not JSR 223. I just wanted to see how Groovy's own
REPL behaves and it seems like it behaves the same, specifically it
seems like every string evaluated has its own lexical scope and that
lexical scope is lost on the next command. So one shouldn't use 'def'
inside groovysh either if one want to declare global variables.

Thanks much for your help!

Mime
View raw message