jastrachan | 30 Mar 12:45 2004
Picon

Re: Minor syntax question.


On 30 Mar 2004, at 11:29, Neil Ellis (Mangala Solutions) wrote:

> Hi John
>
> I was just pointing out a potential compiler problem (ie which syntax
> should it resolve to)
>
>>> c = { param | println("Hello ${param}") }
>>> c('James')
>
> The above is from the groovy website. It shows that any closure is
> callable using the function syntax.
>
> So as you said commands.north returns the closure associated with the 
> key
> 'north' so if I then add brackets I am calling the closure as a 
> function.
>
> This is kinda vague, if I had done a straw poll I would have been
> interested (without people going to their compilers !!) to have seen 
> what
> people would have expected from commands.north().
>
> If the language is going for a JSR then these issues will need to be
> bottomed out.
>
> So the final syntax question I have is does () mean call a closure?

Currently foo.bar() is a method call on the foo object.

If foo is a closure you can just call it via

foo()

However in your case you have a Map. So

map.foo()

only ever calls a method on the map itself. To call a method on a 
closure inside the Map you could do

closure = map.foo
closure()

or maybe (parser allowing)

(map.foo)()

Its not the default behviour of Map to make entries in the map which 
happen to be closures appear as new methods.

However as John says, the Expando class does this - its a dynamic bean 
where you can add new properties and methods dynamically at runtime.

James
-------
http://radio.weblogs.com/0112098/

Gmane