Michael Neumann
11/18/2003 12:06:00 PM
On Tue, Nov 18, 2003 at 06:37:16PM +0900, gabriele renzi wrote:
> Hi gurus and nubys,
>
> Again here to bother you :)
>
> Please note that I'm not here to start a flame. I actually want to
> understand matz' and dev/core choices, please excuse me if I sound
> rude.
>
>
> This is the supposed behaviour for kwd args:
> ---
> def f(a,b: 2 , **kwd)
> p [a,b,kwd]
> end
>
> f(1) #=> [1,2,{}]
> f(1,b:5) #=>[1,5,{}]
> f(1,b:5,c:6) #=>[1,5,{:c=>6}]
>
> ---
>
> I wonder:
> why we introduce the ':' syntax ?
>
> Why can't we call a kwd arg like
> f(1,b=5)
b=5 is an expression and is allowed everywhere to appear.
With your suggestions, this would be no more true.
Matz, instead, introduces new hash syntax:
{b: 5, c: 6} # => {:b => 5, :c => 6}
In Ruby1, we are emulating keyword arguments this way:
f(:b => 5, :c => 6)
With this new hash syntax, it would become:
f(b: 5, c: 6)
Which IMO looks much cleaner.
And you would not break any existent programs, e.g.
f(line=gets, ...)
puts line
would still work.
> I suppose this is to avoid conflicts with normal assignment..
> but what's wrong with making go away the assignment in method calling?
> ?
Expression-orientedness. Any expression should be allowed, even the
following:
def b(k) p k end
x=Object.new
b( begin class << x; def hallo() end; end; x end )
This really distinguishes it from Python, which IIRC is very restricted
in this case (things might have changed).
> OTOH, why we have to define kwd args with
> f(a,b:2)
>
> instead of
> f(a,b=10)
Conflicts with default values when argument is ommited.
A very complex example that will probably be possilbe with Rite:
def foo(a, x=5, *args, b: 42, c: nil, **keys, &block)
end
foo() # => ERROR
foo(1) # => a=1, x=5, args=[], b=42, c=nil, keys={}, block=nil
foo(1, 2, 3, 4, c: 5, port: 8080) {...}
# => a=1, x=2, args=[3,4], b=42, c=5, keys={port: 8080}, block={...}
Maybe too complex, but at least *very* powerful (I don't want to write
the parser for this :-).
> if f(a,b:10) cames in default argument with '=' actually become
> useless?
But b:10 is a keyword argument, whereas b=10 would be a numbered
argument (as in Python).
> I suppose matz refused the current keyword args syntax as it is found
> in python.
> I just wonder why :)
Sorry, I forgot how this is done in python. Can you show an example?
The good thing with matz's keyword arguments is, that it will not break
existing applications. At the first moment, I didn't liked matz's idea,
but now, it think it's really cool.
Regards,
Michael