in reply to How to do multiple inheritance?

The bottom line is that the derived class has to disambiguate access to base class members as required. Consider:

package Foo; my $pkg = __PACKAGE__; sub new { my $class = shift; return bless {$pkg => {basetype => $pkg}}, $class; } sub showBase { my $self = shift; print ref ($self) . ": $self->{$pkg}{basetype}\n"; } package Bar; my $pkg = __PACKAGE__; sub new { my $class = shift; return bless {$pkg => {basetype => $pkg}}, $class; } sub showBase { my $self = shift; print ref ($self) . ": $self->{$pkg}{basetype}\n"; } package Baz; use base qw(Foo Bar); sub new { my $class = shift; my $bar = Bar::new ($class); my $foo = Foo::new ($class); my %self; @self{keys %$foo} = values %$foo; @self{keys %$bar} = values %$bar; return bless \%self, $class; } package Boo; use base qw(Foo Bar); sub new { my $class = shift; my $bar = Bar::new ($class); my $foo = Foo::new ($class); my %self; @self{keys %$foo} = values %$foo; @self{keys %$bar} = values %$bar; return bless \%self, $class; } sub showBase { my $self = shift; Foo::showBase ($self); Bar::showBase ($self); } package main; my $baz = Baz->new (); my $boo = Boo->new (); print "Baz's showBase:\n"; $baz->showBase (); print "Boo's showBase:\n"; $boo->showBase ();

Prints:

Baz's showBase: Baz: Foo Boo's showBase: Boo: Foo Boo: Bar

DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: How to do multiple inheritance?
by perl-diddler (Chaplain) on Sep 18, 2007 at 02:25 UTC
    GrandFather:
    The bottom line is that the derived class has to disambiguate access to base class members as required.
    ----
    *Ding*....that's not the answer I wanted to hear! :-)

    Not sure how it would be "implemented" internally, but my "SuperDeluxe" preference would be to explain to perl that "$s" (handle to left parent) should be used for left-parent accesses, and "$w" (handle to right parent) should be used for right-parent accesses.

    Something on the order of:

    @ISA=qw(Sclass, Wclass); .... new(){ my $s=Sclass->new(); TELL_ISA_LOOKUP_USING(Sclass, 'self') #(self=this class's handle) # and use the "wclass_handle_field as handle for Wclass: TELL_ISA_LOOKUP_USING(Wclass, 'self'->{'wclass_handle_field'}) .... }
    "Seems" like any class other than the 1st parent is a bit like a "second class citizen": can't specify in "use base(...)", and inheritance is only handled automatically for the left-most ISA member. Any other use of multiple inheritance, and it seems like it's mostly "do it yourself". Can't even have "structs" that are nested because of a disallowing of any "inherited" structs (as in "use struct...") -- was going to use that module (actually was using), until I got to next 'struct' further up....then I realized what I wanted to do wasn't supported...*sigh*.

    Thanks much -- I was just hoping I didn't understand something, since it seems like manual disambiguation and making sure correct handles are used with correct ancestor really is _not_ "multiple inheritance" -- since how can "MI" ever work "automatically", if non-primary base classes are always passed the handle to the primary base class? :-(