Perl6 Object Oriented Cookbook (v0.2.1)  
Section 4: Inheritance  
 
Recipe 4.4: Inheriting from Object Instances
Last Updated: Sep 8, 2003
Status: Draft
      Previous Page   Next Page

How important is this problem to you?
  (Login to Vote)
4.50 Rating, 2 Votes  

How acceptable is the proposed solution?
  (Login to Vote)
5.00 Rating, 1 Vote  

Problem:  

You want your class to inherit from an instance of another class.

Solution:  

Just do it:

class MyClass = $parent_obj {
    ...
}

Discussion:

This is an extension of the technique we used in the last recipe, where we subclassed an instance of a builtin type. We can in fact specify any object instance as a parent of a class, with similar effects. This approach is used extensively when creating classless object frameworks.

This technique should be used sparingly, and only when you really mean it. Because the data in the instance can be modified at any time, there's no way for perl to cache even the most basic information about what the inheritance graph looks like, what attributes and what methods are provided by what objects, etc.

Issue: Making changes to a parent instance while in a method of the derived class could have bizzare results. But if you did that, you probably meant to do that.

Issue: This approach is somewhat different from that presented in recipe 3.13, "Creating 'Classless' Object Hierarchies". They need to be reconciled.


Log In to Comment


Login / Edit User Info -- Copyright © 2002 Cognitivity -- Previous Page   Next Page