Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 3D26E200CA4 for ; Wed, 24 May 2017 05:12:04 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 3C385160BD3; Wed, 24 May 2017 03:12:04 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 5E07D160BC3 for ; Wed, 24 May 2017 05:12:03 +0200 (CEST) Received: (qmail 96004 invoked by uid 500); 24 May 2017 03:12:02 -0000 Mailing-List: contact users-help@groovy.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@groovy.apache.org Delivered-To: mailing list users@groovy.apache.org Received: (qmail 95994 invoked by uid 99); 24 May 2017 03:12:02 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd2-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 24 May 2017 03:12:02 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd2-us-west.apache.org (ASF Mail Server at spamd2-us-west.apache.org) with ESMTP id D2B001A003B for ; Wed, 24 May 2017 03:12:01 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd2-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 0.379 X-Spam-Level: X-Spam-Status: No, score=0.379 tagged_above=-999 required=6.31 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RCVD_IN_SORBS_SPAM=0.5, SPF_PASS=-0.001] autolearn=disabled Authentication-Results: spamd2-us-west.apache.org (amavisd-new); dkim=pass (2048-bit key) header.d=gmail.com Received: from mx1-lw-us.apache.org ([10.40.0.8]) by localhost (spamd2-us-west.apache.org [10.40.0.9]) (amavisd-new, port 10024) with ESMTP id JPoHVJ7tNX0T for ; Wed, 24 May 2017 03:12:00 +0000 (UTC) Received: from mail-ua0-f180.google.com (mail-ua0-f180.google.com [209.85.217.180]) by mx1-lw-us.apache.org (ASF Mail Server at mx1-lw-us.apache.org) with ESMTPS id 52A4B5F23E for ; Wed, 24 May 2017 03:12:00 +0000 (UTC) Received: by mail-ua0-f180.google.com with SMTP id y4so83225657uay.2 for ; Tue, 23 May 2017 20:12:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=qzrxBnnjemB3+acpsSxAM51gR/38Me1WeGFt1KcrfKc=; b=J/kw4Ms6GH1qYNLSunUxr5AlwUZK9yHwhYoQVKqx9jO4ywJiL8y2qRoPbTUW0VlaR5 ty/X+SAG/caxc0fB5KYgAODoi0AFbaiANSidLZ6wqQtaNzf+Yyj7zA+skuVfLnkttZt6 l6pUSo2IhN27UgJ6DQbIJL6QlulwdHKi1C2AnO/A2IFmjNBZhJOWe2ZaedaIPaRyRc9n 1HLJKbHpyC7u5DkzEaqQ5R1kSwgyzHQn/HULNR0BtJrLdfrXImDMXIywrtF4cNXPoC7b Gj9tAZzTKrKRhjM2PnPON9whBIfipiMT9dtptRyhcxHzvqKHXahGityTOSV9K6GvhgfF XNng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=qzrxBnnjemB3+acpsSxAM51gR/38Me1WeGFt1KcrfKc=; b=jWdjISVa2JLGzHHWmJm6NRTA+vVWTxxsez7jclo52dAn03nP5uEYWESBj14K3d5cmX egbf/rbgE5uaVNMkudyWF9XQQToLcQaa+APAsrUkL3MXOMO8VLRbcWUZKiiSdweE2ZXy hsuoMDySmBziAHbIi2Fa9+1d2nx2hHVaUIWV4Z7DYVrW4AZfMabkJdeJ9ymPd40YScZh 1Aj1qyNUM2PoIRQR9lPIB/fPuG+uIeEseRtm3mi0uMkcP3RWRLfnlRN0ZbHMvunoAJLX DRHZVDwxQUid8/STR5l05e4HDaCTbip0Vmv6daaj8bYV43HyBPKWlGNbsqT0tOnMyvo7 18dA== X-Gm-Message-State: AODbwcBB3Z3Eb3d6iLqw0dpk20U/DXYvTMkqzKdeOCB3VD1mI4r2JCUd hPWz4wDLbz8yMlkJVn3yLoax9cEI1Q== X-Received: by 10.176.94.133 with SMTP id y5mr18301936uag.150.1495595519836; Tue, 23 May 2017 20:11:59 -0700 (PDT) MIME-Version: 1.0 Received: by 10.176.65.68 with HTTP; Tue, 23 May 2017 20:11:59 -0700 (PDT) In-Reply-To: <4b831d70-202f-4688-3906-df5991e57516@gmx.org> References: <592278B9.7070600@gmx.org> <4b831d70-202f-4688-3906-df5991e57516@gmx.org> From: Assia Alexandrova Date: Tue, 23 May 2017 23:11:59 -0400 Message-ID: Subject: Re: JSR 223 support behavior To: users@groovy.apache.org Content-Type: text/plain; charset="UTF-8" archived-at: Wed, 24 May 2017 03:12:04 -0000 Hi, On Tue, May 23, 2017 at 6:39 AM, Jochen Theodorou 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!