Problem:

You want to use delegation rather than inheritance to
add some capabilities of one class or object to 
another class or object.

Solution: 

Use a PROXY block:

    class MyClass {

    PROXY {
        attr $left_front_wheel is Wheel;
        attr $right_front_wheel is Wheel;
    
        when 'steer'    { $left_front_wheel, $right_front_wheel }
        }
        ...
    }

Discussion: 

A PROXY block is somewhat analogous to the PRE
block of a sub or method. A PROXY block has, or at
least appears to have, an object that represents the
method call as its topic. The object stringifies to the
name of the method. The whens' blocks are a list of
objects that are to receive the message instead of
the object represented by the enclosing class. (That
part could be done other ways; I had to pick a way,
and did so fairly arbitrarily.)

Generalizing, I can imagine a number of dispatch,
marshalling, etc. related blocks at the class definition
level.

Roughly speaking, my idea is to do Damian's Delegation
class dressed using new clothes that look like ones from
the Apos that have come out since he wrote the module
at http://tinyurl.com/1qsk. A longer example follows. (I've
futzed a few things where I didn't understand Damian's
code or I'm not sure of a perl 6 thing.):

    class MyClass {

    PROXY {
        attr $left_front_wheel is Wheel;
        attr $right_front_wheel is Wheel;
        attr $left_rear_wheel is Wheel;
        attr $right_rear_wheel is Wheel;
        attr FlyWheel $flywheel .= new;
        attr MP3::Player $mp3 .= new;
    
        when 'steer'    { $left_front_wheel, $right_front_wheel }
        when 'drive'    { 'rotate_clockwise'     => $left_rear_wheel,
                          'rotate_anticlockwise' => $right_rear_wheel }
        when 'power'    { 'brake' => $flywheel }
        when 'brake'    { / .*_wheel / }
        when 'halt'     { 'brake' => SELF }
        when /^MP_(.+)/ { sub { $1 } => $mp3 }
        when 'debug'    { 'dump' => ATTRS } 
        }
        ...
    }

--
ralph

Reply via email to