Hi Emiliano,

You could do this:

class Person {
    has $.name;
    method new($name) {
       # do some things
       self.bless(:$name);
    }
}

class Employee is Person {

    my $counter = 1;
    method new () {
        # Employees don't have individual identities so we give them a name
:(
        my $new-employee = callwith("employee#{$counter++}");
        # ... do whatever you want to $new-employee here
        return $new-employee;
    }
}

my $e1 = Employee.new();

say $e1.WHAT#-> (Employee)

=====

This works because even in *Person.new()* the *self* is still an
*Employee *type so
when it calls *bless* you still get a *Employee* object instance back. The
*callwith* (which I just noticed is not searchable in docs.per6.org nor is
it mentioned in the context of constructors in the tutorial) is not too
magical, and in this case can be replaced with
self.new(employee#{$counter++}"). It's just there to prevent an infinite
loop if you decide to add an argument to *Employee.new*.

Cheers

LL


On Wed, Oct 28, 2015 at 2:31 PM, TS xx <maringa...@hotmail.com> wrote:

> Hello fellow perl users,
>
> I have been trying to understand perl 6 oop implementation, and one thing
> I still can't figure out is how to call super class methods from lower
> classes.
> Let's say we have two classes, Person and Employee, and the method I am
> trying to access is the object constructor:
>
> class Person {
>         method new ($argument) {
>                 #do some things
>         }
> }
>
> class Employee is Person {
>         method new () {
>                 #call here Person's method new
>                 #do more things
>         }
> }
>
> Can I call the Person's constructor (in non static context), pass the
> required parameter and do more things before returning?
>
>
> Regards,
> Emiliano
>

Reply via email to