On Sun, Aug 22, 2004 at 08:04:48AM -0700, Ovid wrote:
: Sorry if this has already been asked and answered, but in doing a little research 
about Perl 6
: mixins (http://www.perlmonks.org/index.pl?node_id=384858), I ran into some questions 
that I
: couldn't figure from either A12 or "Perl 6 and Parrot Essentials."
: 
: First, how does one access the instance in a constructor?  Let's say I need to 
create a child node
: of a parent node:
: 
:   method new(Class $class: Node $parent) {
:     $:parent = $parent;

You can't set a private attribute before there's an object.  And in general
these things should be done in the BUILD routine, which is an instance
method, rather than the constructor, which is a class method.

:     my $self = $class.bless(0, $parent); # is this correct?

No, has to be named notation.

:     $parent.add_child($self);

That would work since the object is explicit.

:     return $self;
:   }
: 
: I'm also trying to figure out method disambiguation with mixins.  A12 states that 
mixins are to be
: implemented via anonymous inner classes and related via inheritence.  Thus, a later 
mixin will
: override a previous mixin, but this seems to cause the problems we already have with 
mixins:
: 
:   $driver does SanitationEngineer does RaceCarDriver;

That's just a problem with run-time mixins in general if you can't know
in advance that you're going to want both roles.  That's why we
encourage as much of that to be done at compile time as possible.

There is a little hope, though.  You can't do the binding above at
compile time, but you could make a compile-time role that does both
those things and decides how to mediate them, then mix that role in
at run time.

You can also create your own anonymous class at run time that incorporates
both roles simultaneously and disambiguates them.  But you still have to
know that you're going to want both.

: So my $driver who does some racing as a hobby now has no choice but to drive his 
dump truck like
: mad through the streets.  That dump truck had better have darn good brakes.  Is 
there some other
: method (pun not intended, but I'll take it) of disambiguating these?  The delegation 
syntax looked
: promising, but it seemed to be a compile-time tool.

Roles can encapsulate delegation.  Dunno if that helps you.

In my experience, race car drivers *do* drive their trash trucks like
they're in a race.  At least, I assume my local trash truck drivers
are also racers from their behavior...

Larry

Reply via email to