Trans
5/30/2006 4:36:00 PM
Robert Klemme wrote:
> Trans wrote:
> I fail to see the problem.
>
> >> def foo(ha = {}, &b)
> >> (b || ha)[:what_ever]
> >> end
> => nil
> >> foo :what_ever => "123"
> => "123"
> >> foo { |a| "678" }
> => "678"
"Problem" is relative. I can write progams in Assemler too, but I
choode to use Ruby. Right? Here you actually demonstrate what I mean by
showing what one has to do to get around the "problem". THis might not
seem an issue, but consider the use case where your end user if a
developer and you're asking him to define some methods to inteface to
your library. It is not behoving to ease of use, elegance to have to
require this kind of interface and subcode on every such method.
Morover your's using a conincidental similarity between a Hash and Proc
of the method []. THough my particular case generally needs the
polymorphism between Hash and Proc, this is but a specific case. My
point is too the general.
> "Hello" is not a hash like parameter. You don't use a.call in your example.
As I said, my point is too the general case, and I used the simplest
example to that effect I could think of off the top of my head. It has
nothing to do with Hashes per se. And I think that is rather obvious.
> >> def foo(s = nil, &b)
> >> b ? b[] : s
> >> end
> => nil
> >> foo "hello"
> => "hello"
> >> foo { "hello" }
> => "hello"
>
> >> def foo(s = nil, &b)
> >> s || b[]
> >> end
> => nil
> >> foo "hello"
> => "hello"
> >> foo { "hello" }
> => "hello"
>
> Note that this is really a silly (as opposed to "simple") example
> because it does not make sense to provide a block that returns a
> constant value. Also usually a block accepts a parameter so it can do
> something with it.
Actually this comment is silly. "Silly" examples often make the most
obvious demonstrations. Do you really think people use #foo in their
programs? ;-)
> > foo { "Quack" }
> >
> > were ssentially the same as doing:
> >
> > foo( lambda { "Quack" } )
>
> First, that'll break a lot of code.
Which is why I say it is definitely a 2.0 idea.
> Second, you loose easy access to the block.
How is that? You have access via the parameter. Morevoer yield can
still function, so I do see how that that is the case. Hmm... I'm
starting to think I've been misunderstood.
> Third, you cannot provide a parameter *and* a block at the
> same time any more - at least not without having two method parameters
> which defies the purpose of your suggestion IMHO.
Uh? That doesn't make any sense. One parameter can take any object,
including a proc provided via a block. No need for two parameters
--which is exactly what I'm after. Okay I'm pretty sure I'm being
misunderstood now. Tell you waht, I append another post after this one
with a bunch of examples of what I am proposing.
> You forget that you can use normal parameters as they are but need to
> invoke "call" or "[]" on the block. You have to distinguish them anyway.
Not so. Polymorphism/duck-typing allows other objects to respond to the
same methods. Hence I don't have to distinguish them anyway. Indeed,
that's the whole point.
> I'm clearly objecting it as I see too many disadvantages vs. too few
> advantages.
It would appear you have elucidated only one disadvantage, that of
backward compatibily. That is indeed someting to be heavily weighed,
but I fail to see the "many" too which you refer. Please give it some
more thought. And if I haven't explained myself well enough, perhaps my
next post will help.
Thanks,
T.