develooper Front page | perl.perl6.language | Postings from July 2005

Tail method calls, can(), and pre-currying

Thread Next
From:
Brent 'Dax' Royal-Gordon
Date:
July 20, 2005 18:44
Subject:
Tail method calls, can(), and pre-currying
Message ID:
b8b9a511050720184455335664@mail.gmail.com
<brentdax> Is there a Perl 6 tail call syntax, and if so is it
implemented in Pugs?
<autrijus> &sub.goto(...);
<autrijus> and yes.
<brentdax> Oh...are tail method calls possible?
<autrijus> tail method calls. hrmph.

I have a few methods where I'd like to perform tail calls into another
object's methods.  If I were calling one of my own methods, I'd
probably use &meth.goto($?SELF, *@args) as Autrijus suggested before,
but these are calls into another object, based on its runtime class.

As far as I can tell, the easiest way to do this in Perl 6 is thus:

    $obj.can('meth').goto($obj, *@args);

Besides the fact that this isn't possible in current Pugs (which seems
to lack a can() method), it has several other problems: it's too long,
the method name is being treated as a string, the object is included
twice, it will fail silently if $obj doesn't have a `meth` method, and
so on.

One suggestion was a tweak of `can`'s definition: instead of returning
a reference to the method, it returns one with the invocant already
curried into it.  Thus, the above becomes this:

    $obj.can('meth').goto(*@args);

While quite a bit better, this still has many of the problems I
mentioned before.  My recommendation is thus:

    $obj.\meth.goto(*@args);

The .\meth takes a precurried reference to the `meth` method (throwing
an appropriate tantrum if `meth` doesn't exist), which can then be
treated like any other subroutine reference, in this case being
invoked as a tail call.

Of course, this adds *another* piece of syntax to an already large
language, so I'm not sure if it's a good idea.

Am I missing something?  How do you think a tail method call should be
performed?

-- 
Brent 'Dax' Royal-Gordon <brent@brentdax.com>
Perl and Parrot hacker

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About