rj-cole said:
I'm a fan of having optional named arguments, because a lot of the time
functions have a single argument or the arguments come in a well
established order and so you don't want to name them, but other times
it makes the code easier to read if the arguments are named, so for
example if you are instanciating some component and overriding only a
few defaults.
So the problem is how to support both named and unnamed parameters. The
proposals for Ruby 2.0 are work reading.
One kind of obvious thing about the syntax, you need I think some way
to distinguish parameter names from expressions. Usually that ends up
being either commas or brackets or both. It is best to keep it concise
and obvious (ie. simple and following convention to some extent) if at
all possible. And of course you want your interpreter or bytecode to be
fast as well
regards,
Richard.
(Yes, I know I'm repeating myself)
I think the current way of calling a method with "named parameters" is
just fine:
def foo(options = {})
puts option[:bar] || "bar"
end
foo :bar => "bur"
The thing we need is an easier way to work with named parameters in the
method definition. I for one think this is sufficient (note that the
keyword doesn't have to be `named', it could even be a symbol, like `%'):
# `bur' is mandatory, `bar' and `baz' aren't
def foo(named bur, named bar = "bar", named baz = "baz")
puts bur, bar, baz
end
foo :bur => "arr", :bar => "avast", :baz => "matey"
Collection of unknown keys should then be done like for positional
arguments: a `**keys' in the parameter list.
def foo(named bur, named bar = "landlubber", **baz)
baz.each { |key, value| puts "#{key} => #{value}" }
end
foo :bur => "arrr",
irate => "avast ye", :sheep => "baaaah"
-> pirate => avast ye
sheep => baaaah
The reason I think symbols are appropriate as keys when calling a method
with named parameters is that I believe symbols are *names*. Names of
method, variables, attributes, etc. So when I type `foo :bar => "baz"',
I'm calling the method `foo', setting the parameter named `bar' to the
value of `baz' (even though I think the `=>' operator actually means
"points to").
This won't disallow for Matz' `foo bar: "baz"' style. Actually, I think
the `key: value' should be added as a generic way of writing key/value
pairs in a hash, where the key is a symbol (that seems to become the
norm). These should mean the same:
connect :to => "example.com"
connect to: "example.com"
And a normal hash:
{a: "foo", b: "bar", c: "baz"}
-> {:a => "foo", :b => "bar", :c => "baz"}
Cheers,
Daniel