gwtmp01 / mac.com wrote:
> On Nov 2, 2005, at 9:37 AM, Trans wrote:
> > I think you've made this appear more complicated than it is. It's not
> > so magical. A var references an object, so this amount to nothing more
> > than (psuedo)
> >
> >   var.lamda? ? var.call ? var
> >
> > under the hood. And I am sure there is very fast way to do this.
>
> Even if there is a fast way of doing it, I think your proposal requires
> that it be done for *every* expression.  The only way to avoid that
> would be some sort of data-flow analysis to discover which variables
> don't reference one of these Proc-like objects.
>
> But I think there is a bigger problem.  If I'm following you, you
> suggested
> that there would be a special syntax to indicate that the Proc-like
> object *not* be called but instead be returned as a references.  You
> wrote:
>
>    def pass_it_on( x )
>       x  # calls it
>     ->x  # returns it back
>    end
>
> So now you are in a sense reversing the syntax of ruby such that
> you need an explicit marker to *not* call the implied object/method.
> But there is no way to know when you need to use that syntax unless
> you already know that the reference contains this special object.  For
> example
>
 def proxy(a, b, c)
    actual(a, b, c)  # <== HERE
 end
>
> def actual(e, f, g)
>      e + f + g
> end
>
> proxy(dfn; 1; end,  dfn; 2; end, dfn 3; end)
>
> Where do the three "anonymous methods" get evaluated?  In proxy
> before the call to actual or in actual before the call to '+'
> or somewhere in '+' itself?

Since Ruby evaluates expressions in passing args then they'd be
evaluated prior to passing to actual method.

You are right in that there is a caveat for having this in that you
need to know that they are going to be "anonymous methods" in order to
pass them around. But the same is true for calling a proc, i.e. you
have to know it's a proc to call it. In cases of uncertainity a "meta
method" (like block_given?) could be helpful.

 def proxy(a)
    if lambda? a
      actual(->a)
    else
      actual(a)
    end
 end

I imagine a means of supressing all expression evaluations in an
invocation might be possbile too, though I'm not sure how it might be
notated.

> > The use of term is just what its called in the sense that it is not
> > tied to the method table, but to a free varaible. So this is possible:
> >
> >   a = []
> >   a[1] = dfn ; 1 ; end
>
> So it is just an object, which has it's own set of methods.  But one
> of those methods (:call ?) gets executed whenever the reference gets
> evaluated and you can defer the evaluation by prepending '->'.
>
> I don't see how the object you are proposing can be passed around
> since as
> soon as you omit the '->' prefix it is going to get evaluated and
> result in a 'regular' reference to some object.

I see your point. It can be executed implicitly but has to be passed
around explicitly, reverse of current lambda which can be passed around
implicitly but must be executed explicitly. So maybe this would not
make a good wholescale replacement for 1st class lambdas b/c somethings
might be more awkward --I'd have to explore it more to be sure. But if
these "implicit lambdas" can work without any significant pressure on
the interpretor (and as you point out, its imilar to singleton methods
so maybe it can), they could be a useful additional feature.

T.