Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: what would you like to see in perl5.12?

by Joost (Canon)
on Aug 21, 2007 at 23:52 UTC ( #634216=note: print w/replies, xml ) Need Help??

in reply to what would you like to see in perl5.12?

prototypes on methods, so that I can do
$obj->do { stuff(@_) }

hey, a man can dream :-)

update: ysth wanted to know what the prototype for my example was. I'll explain.

First, I'll note I posted this originally because the OP made me think of Re: Automatic Loop Counter not in perl. But that may not be clear enough.

So, anyway, one of the things I do quite a lot is pass around code blocks (or anonymous subroutines subroutine references - in perl it's all the same - as, as far as I'm concerned, it should be)

That means I do a lot of stuff like

$object->method(sub { my ($arg) = @_; # stuff });
Which just looks ugly. Especially when compared to
@result = map { insert(@your_code_here) } @input;
or even
sub one_hundred_and_one_times(&) { my $block = shift; $block->() for (0 .. 100); } @result = one_hundred_and_one_times { more($stuff) };
Sadly, that doesn't work for methods, since method calls ignore prototypes.

In other words, I'd just like

$object->method { # code block };
to pass "sub { # code block }" to "$object->method()" if "method" is declared with a "&" prototype.

updated fixed syntax in example.

Replies are listed 'Best First'.
Re^2: what would you like to see in perl5.12? (meth protos)
by tye (Sage) on Aug 22, 2007 at 01:43 UTC

    That impact of prototypes requires compile-time knowledge which means that this proposal would require compile-time binding of a interface to the scalar variable that will hold the object (and run-time checking that any objects assigned to that scalar implement that interface).

    So this would only be possible with the use of attributes on variables. But that also limits the cases when it works. For examlpe, you could not get a prototype to be in effect for more complex code like $group->get($x)->runcode { ... } unless there was a way to compile-time declare that the variable $group must always implement an interface that includes a get() method that returns an object that always implements an interface that has a runcode(&) method. And you see how complicated that quickly gets.

    For other than the simplest case (calling methods directly via a scalar variable) you need to add a whole lot of static analysis abilities to the Perl compiler.

    I'm not saying that any of this is impossible, or even that it will never happen, but I think it is worth knowing a bit of what would be required.

    - tye        

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://634216]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2018-05-20 18:02 GMT
Find Nodes?
    Voting Booth?