On Sun, May 11, 2008 at 11:53 AM, Eric Mahurin <eric.mahurin / gmail.com> wrote:
>
>
> On Sun, May 11, 2008 at 9:49 AM, Nobuyoshi Nakada <nobu / ruby-lang.org>
> wrote:
>>
>> Hi,
>>
>> At Sun, 11 May 2008 15:14:54 +0900,
>> Evan Phoenix wrote in [ruby-core:16705]:
>> > Not to throw the whole thread into a tizzy again, but why again is:
>> >
>> >   ->(a,b) { }   # stabby proc
>> >
>> > better than
>> >
>> >    def(a,b) { } # anonymous def
>>
>> It would conflict with singleton method definitions.
>>
>> --
>> Nobu Nakada
>>
>
> I've never seen the ()'s used for the "singleton".  i.e.
>
> def (a+b).foo
> ...
> end
>
> Is it necessary to preserve compatibility to do this since nobody really
> uses it?

It could be disambiguated by the grammar distinguishing between

def(<expr>)  or def (<expr>)

as the 'lambda' notation

and

def(<expr>).<identifier>

as defining a singleton method on the value of <expr>


Now there's a slight issue.  It appears that Ruby lets you define a
method with the name def

irb(main):001:0> def foo(a)
irb(main):002:1>    a
irb(main):003:1> end
=> nil
irb(main):004:0> self.foo(^C
irb(main):004:0> quit
Macintosh:rixdvds rick$ irb
irb(main):001:0> def def(a)
irb(main):002:1>   a
irb(main):003:1> end
=> nil
irb(main):004:0> self.def(:foo)
=> :foo

So it would appear that def(x) is ambiguous, is it a definition or an
invocation of self.def(x)?
But if we try to use it (this is in irb 1.8):

irb(main):005:0> def(a)
irb(main):006:1> a+1
irb(main):007:1> end
SyntaxError: compile error
(irb):5: syntax error, unexpected '\n', expecting tCOLON2 or '.'
(irb):7: syntax error, unexpected kEND, expecting $end
        from (irb):7

It clearly seems to be parsed as a method definition.

So I think def(<expr>) <block>  could be unambigously defined to be an
expression evaluating to a lambda.  Whether or not the two (or is it
three) meanings of def can be explained clearly enough is a slight
question.

I'd actually prefer

   lambda(x,y) {...}
to
  def(x,y) {...}

but this would seem to have less easily reconcilable conflicts with
the existing lambda method.

-- 
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/