From: David Alan Black Date: 2001-08-13T23:07:51+09:00 Subject: [ruby-talk:19638] Re: () overloading Hello -- On Mon, 13 Aug 2001, Avdi Grimm wrote: > On 13 Aug 2001 19:25:09 +0900, Ed Sinjiashvili wrote: > > aProc = Proc.new {|a, *b| b.collect {|i| i*a }} > > aProc(9, 1, 2, 3) # [9, 18, 27] > > I'll second that! Coming from a C++ background, there's something that > makes me feel all warm and fuzzy to know that my closures and function > objects can have the exact same syntax as "real" functions. It makes it > easier to replace a function with a functor during refactoring, for one > thing. Also it means that down deep within a subroutine I don't have to > think about unimportant details like "this 'foo' is a method, and needs > to be called /this/ way; but 'bar' here is an object, and needs to be > called /that/ way" - I know that they are both callable, and so can just > use the same syntax on both. I think people from a Python background > would like this as well, since Python has the option to overload () too. I'm afraid I don't use C++ or Python, so I'll have to fall back on speaking from a Ruby background. I hope that's OK :-) You're abstracting the quality "callable" from both methods and procs, in equal measure, and inferring that the different syntax is an anomaly. But that's a self-validating argument. There's actually no "callability" that methods and proc objects share, at least at the same level of indirection. "Calling a proc" is shorthand for "sending the message 'call' to a Proc object", while "calling a method" is shorthand for "sending the message 'method_name' to an object". To make the point in a somewhat over-the-top way: If you dismiss the fact that a proc needs to be sent 'call' as an irrelevant nuisance, then you might as well say that we should be able to put parentheses after strings: "puts 'hi'"() instead of having to do: eval "puts 'hi'" since the effectiveness of the eval approach demonstrates that the string is "callable" :-) Also, consider the following: pr = proc {|x| puts "I'm a proc, argument is #{x}" } def pr(x) puts "I'm a method, argument is #{x}" end p pr # # pr(123) # I'm a method... pr.call(123) # I'm a proc... If you collapse the method() and proc.call() syntax, what would "pr(123)" mean? Besides, there *is* Proc#[] :-) David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav