mop problem 1 - mop can't have protected attribute variable

mop can't have protected attribute variable. All attribute variable is private in the class.

use mop;

class Point {
has $x; # private attribute variable
has $y; # private attribute variable
}

class Point3D extends Point {

method foo {
# We can't access $x and $y.
}
}

If object is hash based object, this is resolved by the following way.

class Point {
  
   method init {
    $self->{_x} = 3;
  }
}


class Point3D extends Point {

method foo {
my $x = $self->{_x};
}
}

hash based object can communicate super class and sub class.
but mop is inside-out object. this is impossible.
mop only allow private access via attribute variable or public access via accessor method.
this is not useful.

4 Comments

That's what accessors are for.

use mop;
 
class Point {
  has $!x is rw;
  has $!y is rw;
}
 
class Point3D extends Point {
  method foo {
    # We can't access $!x and $!y.
    # But we can access $self->x and $self->y.
  }
}

I agree that accessors should only be created for the public interface. I think perhaps where we differ is that I think subclasses should only use their parent's public interface!

Leave a comment

About Yuki Kimoto

user-pic I'm Perl Programmer. I LOVE Perl. I want to contribute Perl community and Perl users.