The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Better Keyword Arguments

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Daniel Berger

Posts: 1383
Nickname: djberg96
Registered: Sep, 2004

Daniel Berger is a Ruby Programmer who also dabbles in C and Perl
Better Keyword Arguments Posted: Oct 18, 2005 7:28 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Daniel Berger.
Original Post: Better Keyword Arguments
Feed Title: Testing 1,2,3...
Feed URL: http://djberg96.livejournal.com/data/rss
Feed Description: A blog on Ruby and other stuff.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Daniel Berger
Latest Posts From Testing 1,2,3...

Advertisement
One of the things Matz discussed during his keynote was keyword arguments for Ruby 2.0. His proposal went something like this:
def foo(a, b=0, c:4)
   ...
end

foo(1, 2, c:3) # a=1, b=2, c=3

I took an immediate dislike to this proposal. The first problem I have with it is that you're forced to make a declaration about How Things Work in the method definition. The second problem I have is that I'll be forced to lookup the method definition in order to see which parameters allow keyword arguments and which don't. That, or we'll need a major change to rdoc.

Things went from bad to worse with the *rest and **keyword syntax. Oofda.
def foo(*rest, a:4, b:0, **keys)
   ...
end

baz(1, 2, b:2) # rest=[1,2,{b:2}], a=4, b=2, keys={b:2}

This makes my head want to explode. It's a complete break from current Ruby syntax, looks like complete hell, and is utterly untuitive. It's just overwrought.

The ideal solution is to allow users to use keyword parameters without having to do anything special whatsovever in the method definition, and allowing keyword parameters to be used for all variables, in addition to allowing positional parameters:
# A standard Ruby method declaration
def foo(a, b, c=4)
   ...
end

# Ideal syntax
foo(a:1, b:2, c:3) # a=1, b=2, c=3
foo(a:1, b:2)      # a=1, b=2, c=4
foo(1, 2, c:3)     # a=1, b=2, c=3
foo(1, 3, 3)       # a=1, b=2, c=3

As for *rest parameters, the ideal situation is to eliminate **keys altogether (yuck) and keep the current behavior where *rest arguments come at the end of the method declaration. For keyword parameters slurped into a *rest variable, they are collected as a hash:
def foo(a, b, *c)
   ...
end

foo(1, 2, 3)            # a=1, b=2, c=3
foo(1, 2, foo:4, bar:5) # a=1, b=2, c=[{'foo'=>4, 'bar'=>5}]
foo(1, 2, 8, baz:5)     # a=1, b=2, c=[8, {'baz'=>5}]

Clean. Sensical. No extra work in the method declaration. Keeps the current rules in effect with regards to *rest variables. None of that **keys nonsense. Even forces you to use good variable names for people reading the generated documentation who need to know what the valid parameters are.

Wouldn't that be awesome?

Now what if I told you that it's already in Sydney?

>:)

Read: Better Keyword Arguments

Topic: Matz Wild and Crazy Keynote Previous Topic   Next Topic Topic: Behaviour-Driven Development

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use