jastrachan | 25 Mar 09:25 2004
Picon

Re: something about map


On 25 Mar 2004, at 07:42, bing ran wrote:
> I see. So anytime names parameters are used in function call, the 
> internal implementation is to translate this to pack them up in a map 
> and pass it in. i.e.:
>
> f(a:1, b:2) equals to f(['a':1, 'b':2])

Yep. Right now thats the case, we may get more clever later on like 
unpacking the logical Map and passing in normal parameters.  e.g. if 
you'd defined the function like this

f(a, b, c=3, d=4) { ... }

we could maybe one day support

f(a:1, b:2)

which would be equivalent to

f(1, 2, 3, 4)

> Then the next question is how are the the passed in values accessed 
> inside the function:
>
> > def f(p) {println "${p.a} + ${p.b}"}

assert p instanceof Map

> a = 'aa'
> b = 'bb'
> f(a:1, b:2)
> f(['a':1, 'b':2])

Note neither of the above actually use the a and b variables.

> > go
>
> 1 + 2
> 1 + 2
> The result shows that the a and b are never treated as variables. 
> One has to be fluent with named parameters dialect to avoid feeling 
> surprised.

Yep. Though note that if you did this...

f( [ a : 1, b: 2 ] )

you'd get

null + null

since the above is equivalent to

f( ['aa':1, 'bb':2] )

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

Gmane