Front page | perl.perl5.porters |
Postings from May 2008
Re: thoughts about overloading method calls
Thread Previous
|
Thread Next
From:
Ricardo SIGNES
Date:
May 7, 2008 04:40
Subject:
Re: thoughts about overloading method calls
Message ID:
20080507114001.GA7254@knight.local
* Rafael Garcia-Suarez <rgarciasuarez@gmail.com> [2008-05-07T03:33:40]
> 2008/5/6 Ricardo SIGNES <perl.p5p@rjbs.manxome.org>:
> > What if, instead, I could overload method calls on an object?
> >
> > package Classless::Root;
> > use overload method => 'invoke_method';
>
> use overload '->' => \&invoke; # more graphic ?
I thought about that, but I'm concerned that it looks like it might handle
$obj->{foo} and $obj->[0].
> * that would work only for instance methods, not class methods
I think that's totally fine. I think, in fact, that packages will be used to
define behaviors (dare I say, metaclasses) and that classes will be objects of
one sort and instances objects of another:
$class = Class->new; # we get a Class object
$obj = $class->new; # we get a (say) Class::Instance object, or maybe a
Class with is_instance true. not a worry to me
$obj2 = $obj->new; # method missing!
> * that would step over AUTOLOADed methods -- AUTOLOAD would never
> be called in presence of -> overloading
That's fine. AUTOLOAD is part of the Old Style Method Dispatch. The method
overload handler would get every single method call on instances. The only
exception is th overload handler itself:
use overload method => 'some_handler';
$obj->foo(1,2,3);
Now (ref $obj)::some_handler($obj, [ 1, 2, 3 ]) might be called (but as an
old-style method) or we might call Class->some_handler($obj, $args). Not sure.
Calling it on class might be simpliest, since the class MUST use old-style
dispatch.
We have to consider this case:
package Class::Newstylee;
use overload
method => ...,
'""' => 'method_name',
fallback => 1;
Does C<"$obj"> trigger $obj->method_name (overloaded dispatch) or
$obj->method_name (old dispatch) or Class->method_name($obj) (yuck).
> * would that step over regular, existing, defined methods ?
> If it doesn't, we can instruct invoke() to install new methods,
> à la AUTOLOAD.
Yes. *All* instance methods go to the handler.
> * We will need to avoid calling methods on the same object from inside
> invoke(), to avoid deep recursion.
At the very least, we need to keep recursion in mind.
> * What if someones invokes the "(->" method directly ? (the
> internal name of "invoke()".) Maybe all overload methods
> (whose names begin with "(") should be excluded from this
> mechanism.
I didn't know you could do this, so I haven't thought about it and have
absolutely no idea yet.
> * We will probably want a nice way to throw the error "Can't locate
> object method %s via package %s", like a new builtin, or a core
> function in the overload package
Yes! I really want Class::WhiteHole's functionality to be easy to get.
> * Of course, subclasses will inherit that overloading.
As it should be! You can use old-style dispatch to implement a derived set of
new-style dispatch behaviors!
--
rjbs
Thread Previous
|
Thread Next