From: dblack@... Date: 2007-02-06T07:33:20+09:00 Subject: Re: Proc vs lambda vs proc Hi -- On Tue, 6 Feb 2007, Brian Candler wrote: > On Tue, Feb 06, 2007 at 04:00:44AM +0900, Pit Capitain wrote: >> gwtmp01@mac.com schrieb: >>> (... useful infos about procs ...) >> >> Very interesting! Thanks for sharing your experiments. > > Yes, it's interesting to be reminded of all this again. > > Much as I love Ruby, I hate having multiple versions of essentially the same > concept which differ in subtle ways. I would find it more aesthetically > pleasing if a tiny Ruby core could bootstrap the whole language - and it > might make it easier to port (and understand). > > Now, I wonder why you couldn't approach this problem from the other > direction: instead of having two or more different types of lambda, which > handle 'return' semantics differently, have a single type of lambda and two > different return statements. For example: > > return val # return from enclosing method > result val # return from this block only > > Internally, you could implement 'return' semantics using catch and throw: > > def foo > ... some code > end > > # could be implemented as something like this: > > define_method(:foo) do > catch(:return) do > ... some code > end > end > > module Kernel > def return(rc=nil) > throw(:return, rc) > end > end > > The other difference concerns arity. I don't see why argument checking can't > be done everywhere, as long as you have the option of explicitly allowing > variable arguments where you want them: > > p1 = Proc.new { |x=nil,y=nil| ... } # accept 2 or fewer args > p2 = Proc.new { |x, y, *z| ... } # accept 2 or more args > p3 = Proc.new { |*x| ... } # no more single-arg special case! > > That is, use the same argument list handling for a 'def' method definition, > a block, and a Proc/proc/lambda. I think the reason for there not being default arguments in code blocks is the ambiguity of the pipe character: {| x=1 | 2 | 3 } # {(x=1) 2 | 3 } or {(x=1|2) 3 } ? Dave Thomas suggested something at RubyConf 2005 along the lines of: def m(a,b,c) # method end def (a,b,c) # anonymous function end I'm not sure what happened to that; I haven't seen it mentioned much in discussions of this stuff. David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com)