jastrachan | 25 Mar 07:58 2004
Picon

Re: something about map

First a little bit of background.

Map syntax is

[ keyExpression : value, keyExpression : value ]

e.g.

[ 1 : 4, 'abc' : 423 * 12, someMethod(1234) : 'whatnot' ]

So ...

On 25 Mar 2004, at 01:14, bing ran wrote:
> Hi, can someone help me understand what I have seen in an interactive 
> session with regard to calling functiond with a map?
>
>
> 1> def f(m) {println m}
> 2> f([a:1, b:2])

[a:1, b:2]

if a and b are both null, will create a map looking like...

> [null:2]

Give a and b different values and it'll look different :)

>
> 1> def f(m) {println m}
> 2> f(a:1, b:2)
> 3> go
>
> [a:1, b:2]

Now we're using 'named parameter call' syntax, where the tokens a and b 
are referring to named parameters of the method, rather than local 
variables a and b.

>
> 1> def f(m) {println m}; f('a':1, 'b':2)
> discarding line:1: expected ")"; found ':' at 1:28
>    def f(m) {println m}; f('a':1, 'b':2)
>                               ^
> 1> def f(m) {println m}; f(['a':1, 'b':2])
> 2> go

This is a syntax error, since named parameter passing syntax (which 
looks map-ish) is of the form

method( identifier : value, identifier : value)

and 'a' is not an identifier

So this is fine

method(cheese:'Edam')

this is not

method('cheese':'Edam')

Though you could pass in a map as a parameter value

method( ['cheese':'Edam'], somethingElse )

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

Gmane