> I like this too. And there's no reason IMO why we can't have both
> anonymous functions and anonymous closures (so def and lambda could have
> the same syntax but lambda would create a closure, while def would not).I agree. See the thread "Anonymous methods, blocks etc. (Cont.
'default block params')" starting at ruby-talk/160709 for some recent
discussion about this.
[ruby-talk:150709] is what I think you meant. The discussion actually
stems from the ruby-dev summary in [ruby-talk:150581].
> Then we could also have named closures as well:
>
> class Foo
> a = 42
> lambda foo(b, c)
> puts a, b, c
> end
> endThat's an interesting idea. It would certainly be orthogonal. However,
you can already do that with define_method:class Foo
a = 42
define_method(:foo) do |b, c|
puts a, b, c
end
endFoo.new.foo(1,2)
__END__
42
1
2
define_method takes a block, which currently passes arguments by
multiple assignment. That's not what we want. You are right that we
could use define_method, but I think it would look more like this:
a = 42
l = lambda (b, c=10) { puts a, b, c }
define_method(:foo, &l)
> Currently, the following code works fine:
[snip]
> But I would imagine that if lambda were a keyword this would confuse the
> parser.Well, I doubt it would break ~much~ code. It would be quite an unusual
thing to define a function named lamdba(), no?
I agree that it would not break much code. But it must be considered,
even if this is the conclusion.
[snip discussion about || vs <> block local syntax]
I think you're on a losing wicket here. It seems that the zeitgeist is
with the semi-colon to introduce block locals.
My point was that the distintion we want to make is between passing
block arguments through multiple assignment and passing block arguments
method style, rather than between anonymous procs and anonymous
functions/closures. The two differ only in how their arguments are
passed; the syntax IMO should reflect that.
I wasn't intending to make a point about block-locals; I thought they
were a side-effect (but perhaps I was wrong).
With argument passing through multiple assignment (|...|), it is okay
for block parameters to not be block-local. The result is that the
variable gets assigned. Matz has previously stated that all block
parameters will be block-local; he may change (or have already changed)
his mind; I pointed it out because it may or may not remain as is.
With passing arguments method-style (what I was using <...> for, though
I know that syntax has some issues), block parameters probably shouldn't
be block-local. My thought was that if you're not going to use multiple
assignment for argument passing, you shouldn't use assignment at all.
The only way around the assignment is to create a new variable that is
block-local.
Paul
···
On Thu, Oct 20, 2005 at 05:33:37AM +0900, Sean O'Halpin wrote:
On 10/19/05, Paul Brannan <pbrannan@atdesk.com> wrote: