On Tue, 25 Oct 2005, Louis J Scoras wrote:

> On 10/24/05, Trans <transfire / gmail.com> wrote:
>>
>> Widdle it:
>
>
> *agrees*
>
> def foo( required :bar, :baz ; optional :request=>'useless' ; named
>> :side, :meat=>'fish' )
>>
>> def foo( bar, baz ; optional :request=>'useless' ; named :side,
>> :meat=>'fish' )
>>
>
> That one is very much like the lisp defun if I'm not mistaken.
>
> def foo( bar, baz, request='useless', keys: side, meat=>'fish' )
>>
>> def foo( bar, baz, request='useless', side=>nil, meat=>'fish' )
>
>
> That looks reasonable. What's wrong with that? In the calling code it
> wouldn't be ambiguous because there's: a fixed number of required
> parameters; optional parameters come after the required ones, and any
> keywords are specified with '=>' as they are currently.

not really, think about it

   request = 'side' => 'beans', 'meat' => 'tofu'

   foo 'bar', 'baz', request

have you passed request as 'request' or the keywords?  how bout here:

   foo 'bar', 'baz', 'side' => 'beans', 'meat' => 'tofu'

and here

   foo 'bar', 'baz', nil, 'side' => 'beans', 'meat' => 'tofu'

is request 'nil' and therfore is not assigned the default value?  or is nil
clobbered with the default?  if it's clobbered how can you ever call a method
that accepts an optional argument with default with a value of nil and not
have it clobbered?  eg. how could you do this:

   def foo optional = 42
   end

how do you call it with optional == nil?

if this works

   foo nil

then this cannot work

   def foo required, optional = 42, keyword => 'forty-two'
   end

since you cannot call foo with both a keyword AND get the default value for
optional.  this won't work

   foo 42, 'keyword' => 40 + 2

since you are simply passing a hash as the value for optional. and keyword is
not set - and this won't work

   foo 42, nil, 'keyword' => 40 + 2

since now optional is nil and not the default value of 42.

i think you cannot ever have both trailing optional arguments AND keywords
with current ruby calling conventions since it will always be impossible to
determine whether the last hash is a value for an optional argument or the set
of keywords.  the only way is to have a syntax that calls methods with
keywords that are not an inlines hash, eg

   foo 42, keyword : 42.0   # optional gets default value
   foo 42, keyword => 42.0  # optional IS a hash , keyword is not given

when i was coding parseargs all this became apparent very quickly.  trying to
write a parser for any proposed syntax is a good exercise that shows how nice
a real keyword syntax might make things.

regards.

-a
-- 
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| anything that contradicts experience and logic should be abandoned.
| -- h.h. the 14th dalai lama
===============================================================================