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

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

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

Problem:  

You want to declare that a class may not be subclassed or modified.

Solution:  

Use the is final property:

class MyClass is final {
   method foo(...) { ... };
   method bar(...) { ... };
}

Discussion:

There are circumstances in which you want to declare that the given implementation of a class is "final"; that is, that nothing about the class can be modified by subclasses, and that any attempt to do so should result in an exception. The is final property enforces this.

Note that you can still create an alias to the class, which has the same effect as creating an empty, non-modifiable subclass.


Log In to Comment


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