Perl6 Object Oriented Cookbook (v0.2.1)  
Section 3: Working with Classes  
 
Recipe 3.1: Declaring a Class as Abstract
Last Updated: Sep 8, 2003
Status: Very Likely
      Previous Page   Next Page

How important is this problem to you?
  (Login to Vote)
4.75 Rating, 4 Votes  

How acceptable is the proposed solution?
  (Login to Vote)
3.75 Rating, 4 Votes  

Problem:  

You want to declare that a class is abstract; that is, that any attempt to instantiate the class directly (as opposed to one of it's subclasses) should result in an immediate exception.

Solution:  

Declare the class using the is abstract property:

class MyClass is abstract {
    ...
}

Discussion:

Abstract classes are classes that are intended to be used as base classes for others to inherit from, but that shouldn't be directly instantiated themselves (typically because they aren't fully functional without the behaviors added by subclasses.) In Perl6, this is accomplished using the is abstract property. Attempting to instantiate a class that has been declared is abstract results in an exception; instead, you must always instantiate one of the non-abstract subclasses of the class.

Just because a class does not have a constructor method does not imply that a class is abstract. Perl6 does not require constructors to exist (it can assume null-operationed constructors.)

Note that abstractness, as a property, does not "inherit" downward: a subclass of a class is not automatically abstract, because that's almost certainly not what you want. All classes are assumed to be non-abstract (e.g. concrete) unless they are specifically declared with the is abstract property.

The is abstract property is different from the yada-yada-yada operator, "...". The latter is merely a placeholder: it declares that the class will have an implementation, but that the implementation will be specified later in the code.


Log In to Comment


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