You can put things into the binding yourself if that helps: ------------ import org.codehaus.groovy.jsr223.*
def engine = new GroovyScriptEngineFactory().scriptEngine engine.put('something', 10) engine.eval('println something') ------------ Cheers, Paul. On Tue, May 23, 2017 at 8:39 PM, Jochen Theodorou <blackd...@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". > > 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 > > 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... > > bye Jochen >