Perl6 Object Oriented Cookbook (v0.2.1)  
Section 5: Constructors, Destructors, and Autochained Methods  
 
Recipe 5.2: Declaring Initializers
Last Updated: Sep 8, 2003
Status: Draft
      Previous Page   Next Page

How important is this problem to you?
  (Login to Vote)
0.00 Rating, 0 Votes  

How acceptable is the proposed solution?
  (Login to Vote)
0.00 Rating, 0 Votes  

Problem:  

You want to create one or more initializers for your class: methods that, when called on a newly created object, initialize that object.

Solution:  

Use the init keyword to create the methods:

class MyClass is ParentClass {
    has $.count = 0;
    
    init ( ... ) { ... }
    init ( ... ) { ... }
    init ( ... ) { ... }
}

Discussion:

Initializers in Perl6 are defined using init. Initializers are autochained, such that any applicable initializers in parent classes are invoked before your subclass initializer is called.

Initializers may be multimethods: there may be any number of initializers, each accepting different arguments and initializing the object in different ways.

(This recipe is a placeholder, until it is determined whether 'init' is different from 'new' in Perl6 classes.)

Issue: It is actually fairly dubious that initializers could be effectively autochained, if they are allowed to have arguments (since the arguments for parent classes and subclasses wouldn't necessarily match.)


Log In to Comment


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