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: 

The PROXY block behaves as if the following were
true. The block has a topic. The topic is an object
that represents a method call. This object stringifies
to the name of the method called. The block is called
each time a method call is invoked on the enclosing
class, or an instance of the class. The whens' blocks
are a list of things that are proxies for (ie delegatees
of) the matched call.

------------

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.

    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 } 
        }
        ...
    }

or something like this.

--
ralph

Reply via email to