Perl6 Object Oriented Cookbook (v0.2.1)  
Section 2: Attributes and Methods  
 
Recipe 2.8: Creating Abstract Methods
Last Updated: Sep 8, 2003
Status: Very Likely
      Previous Page   Next Page

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

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

Problem:  

You want to declare that a given method exists, and perhaps what its signature is, but you want to leave the definition of that method to subclasses.

Solution:  

Declare the method using the is abstract property:

class MyClass {
    method do_something is abstract;
}

Discussion:

Abstract methods or classes in perl6 are created by using the is abstract property. It specifies that no implementation has been given; the usual intent is to provide an actual implementation later, in subclasses.

If an abstract method is actually called (that is, if the method is ever called in a situation in which the method has still received no implementation) an exception is thrown.

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

Note that simply neglecting to provide implementations for a method or class is a syntax error in Perl6: use either is abstract or ... as a visual cue that the method indeed has no implementation (i.e. that you really meant to do that.)


Log In to Comment


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