phkim | 1 Oct 06:29 2004
Picon

Is the binding (in closure) dynamic or static?

Hi eveyone,

g = { |y| return x+y }
f = { |x| return g(1) }   // Will x be binded in the closure g ?
assert f(17) == 18        // For the dynamic binding, this should be passed.

A reference about closure is (see the section 6.6):
     http://www.cl.cam.ac.uk/users/lcp/papers/Notes/Founds-FP.pdf

Regards,
Kim

Chris Poirier | 1 Oct 08:46 2004

Re: Is the binding (in closure) dynamic or static?

Hi Kim,

> g = { |y| return x+y }
> f = { |x| return g(1) }   // Will x be binded in the closure g ?
> assert f(17) == 18        // For the dynamic binding, this should be passed.

x in closure g refers to a variable that either exists only in the body
of g or that is visible in the same scope as g and f.  The x in closure
f exists only in f and hides any outer x for the body of f.  Therefore,
if this sample was run as a script, assert f(17) == 18 should get you a
compile error, a runtime error, or false, depending on how the language
actually handles undefined variables (the x in closure g is not
defined).  As to what the implementation currently does . . . I don't
have a build handy, so you'll have to check for yourself.

Later,
   Chris.

John Wilson | 1 Oct 09:22 2004
Picon

Re: Is the binding (in closure) dynamic or static?


On 1 Oct 2004, at 05:29, phkim@... wrote:

> Hi eveyone,
>
> g = { |y| return x+y }
> f = { |x| return g(1) }   // Will x be binded in the closure g ?
> assert f(17) == 18        // For the dynamic binding, this should be 
> passed.
>

A Groovy closure is a closure over all the variables accessible at the 
point of closure declaration

So the x in your closure g is unbound  as there is no variable x 
accessible when the closure is created

Even though there is a variable x which is accessible at the point it 
is used in the closure f the closure g will not use it

Running this code will cause an exception to be thrown.

There are a couple of Groovy features which complicate the issue:

1/  As Groovy is a dynamic language it is not possible to always tell 
if x can be resolved in the scope where the closure was created until 
the closure is evaluated.
For example Groovy has a class called Expando which can have new 
properties and methods added dynamically. If a closure is created 
within a subclass of an Expando it is possible that a reference which 
(Continue reading)

Josh Juneau | 1 Oct 13:04 2004

Groovy Swing Reference?


Does anyone know if there is a reference card for Groovy Swing, or perhaps a
site I can go to for a complete compilation of Groovy Swing syntax?  I am
interested in learning, but the differences between Groovy Swing and regular
Java Swing are prohibiting me from doing so.  It would be much easier with a
reference.

Thanks-

Josh

Marc Hedlund | 1 Oct 17:11 2004

Re: Groovy Swing Reference?


I'm planning to put together a tutorial in the next few days.  I'd be 
happy to answer questions in email if I can (might help me guide the 
tutorial!).  Also, I found the source of SwingBuilder helpful as a 
reference:

<http://cvs.groovy.codehaus.org/viewrep/groovy/groovy-core/src/main/groovy/swing/SwingBuilder.java?r=1.9>

Marc Hedlund 
e: marc at precipice dot org

On Fri, 1 Oct 2004, Josh Juneau wrote:

> 
> Does anyone know if there is a reference card for Groovy Swing, or perhaps a
> site I can go to for a complete compilation of Groovy Swing syntax?  I am
> interested in learning, but the differences between Groovy Swing and regular
> Java Swing are prohibiting me from doing so.  It would be much easier with a
> reference.
> 
> Thanks-
> 
> Josh
> 
> 

Philippe Ombredanne | 1 Oct 18:42 2004

Groovy fresh meat, really ultra fresh! spread the love!

All,
Groovy has now its own very page on Freshmeat:
http://freshmeat.net/projects/groovy
If you like Groovy, go there, subscribe to it, rate it high, 
comment back, add a dependency on your project that 
use Groovy, or categorize your project as using the 
Groovy language.

Sourceforge and Java.net are considering similar additions soon.

Let's spread the love!
--

-- 
Cheers
Philippe

philippe ombredanne | nexB - Open IT Asset Management 
1 650 799 0949 | pombredanne at nexb.com 
http://www.nexb.com

phkim | 1 Oct 18:12 2004
Picon

Passing variables into a nested closure

Hi eveyone,

I have tested on Groovy 1.0 beta 7.
Here is my test code:

println "List in a nested closure"
x = []
c2 = { x << 5 }
c2.call()
println "x = ${x}"   # [5]
c1 = { c2 = { x << 3 } }
c1.call()
c2.call()
println "x = ${x}"   # [5, 3]
println "----------------"

println "StringBuffer in a nested closure"
x = new StringBuffer("")
c2 = { x << "a" }
c2.call()
println "x = ${x}"   # a
c1 = { c2 = { x << "J" } }
c1.call()
c2.call()
println "x = ${x}"   # aJ
println "----------------"

println "String in a nested closure"
x = new String("")
c2 = { x += "a" }
(Continue reading)

Josh Juneau | 1 Oct 19:16 2004

RE: Groovy Swing Reference?

Excellent!  I look forward to seeing your tutorial when it is available.

I had previously seen the link that you sent me, I guess that is the best
way to figure out what is going on...just check out the code.  I really like
the Groovy Reference Sheet which is available on the main site...that is
what I would love to see for Groovy Swing.  I use the reference sheet quite
often since I am always jumping from PL/SQL to Java to Groovy and back.  It
is nice to have a "guide"

Thanks again.

-----Original Message-----
From: Marc Hedlund [mailto:marc@...] 
Sent: Friday, October 01, 2004 10:11 AM
To: user@...
Subject: Re: [groovy-user] Groovy Swing Reference?

I'm planning to put together a tutorial in the next few days.  I'd be 
happy to answer questions in email if I can (might help me guide the 
tutorial!).  Also, I found the source of SwingBuilder helpful as a 
reference:

<http://cvs.groovy.codehaus.org/viewrep/groovy/groovy-core/src/main/groovy/s
wing/SwingBuilder.java?r=1.9>

Marc Hedlund 
e: marc at precipice dot org

On Fri, 1 Oct 2004, Josh Juneau wrote:

(Continue reading)

ben | 1 Oct 13:54 2004
Picon

Embedding Groovy

I'm trying to build a little GUI where I can execute interactively.  Do we have
to grab stdout manually?  Wouldn't it be nice if the BSF had a method that
returned all output, as opposed to just the result of the evaluation?

Thanks!

John Wilson | 1 Oct 19:48 2004
Picon

Re: Passing variables into a nested closure


On 1 Oct 2004, at 17:12, phkim@... wrote:

>
> Here is the result:
>
> List in a nested closure
> x = [5]
> x = [5, 3]
> ----------------
> StringBuffer in a nested closure
> x = a
> x = aJ
> ----------------
> String in a nested closure
> x = a
> x = a
> ----------------
> Number in a nested closure
> x = 5
> x = 5
> ----------------
>
>
> As above, String and Number objects play differently.  Why?
>

That's a really interesting result!

It's a very odd bug.
(Continue reading)


Gmane