Michael G Schwern <[EMAIL PROTECTED]> writes:
> On Mon, Sep 04, 2000 at 09:53:39PM -0000, Perl6 RFC Librarian wrote:
> > Objects : Core support for method delegation
>
> I like it! One gripe (of course)...
>
>
> > The proposed delegation mechanism would work via a pragma:
> >
> > use delegation
> > attr1 => [qw( method1 method2 method3 )],
> > attr2 => [qw( method4 method5 )],
> > attr3 => [],
> > attr4 => [],
>
> I will often use a more complicated data structure for my objects,
> often organizing all sub-objects into a hash of hashes...
>
> $obj->{locks}{MacOSX} = $macosx_obj;
> $obj->{locks}{Mac} = $mac_obj;
> $obj->{locks}{BSD} = $bsd_obj;
>
> which is nice when you stuff alot of things into an object. If I
> wanted to deligate to those objects in $obj->{locks}, how would I
> under your proposal?
Flatten the hierarchy? Make your aggregations into classes themselves
and set up delegation rules there?
>
> In a similar vein, I can see a use for wanting to deligate a set of
> methods to an entire list of objects. Consider...
>
> $obj->{locks} = [$macosx_obj, $mac_obj, $bsd_obj];
>
> it would be nice to be able to state that "method1" should deligate to
> each object in the $obj->{locks} list until it is found.
package ListOfObjects;
use strict;
use Symbol qw/gensym/;
sub new {
my($class) = shift;
my $self = bless {}, ref($class) || $class;
$self->push(@_);
}
sub push {
my $self = shift;
while (shift) {
my $attr = gensym;
$self->{$attr} = $_;
use delegate $attr => [];
}
}
> Also, what happens when a deligated attribute does not contain an
> object when Perl checks? Should it produce a warning? I'd say no. I
> can easily see cases where you'd like to be able to deligate to
> objects which may or may not be instanciated at run-time. If a
> warning was issued, it would be difficult to circumvent. You'd have
> to place a dummy object in that slot.
You know, this may be a case for Mister Fowler's RFC about auto
instantiated objects. Except C<Dog $self->{attr}> isn't actually valid
syntax is it? There's certainly a case for just using the (singleton)
null object as a placeholder until a real object comes along, and it
can solve a host of other problems.
--
Piers