John M. Dlugosz wrote:
Audrey Tang audreyt-at-audreyt.org |Perl 6| wrote:
John M. Dlugosz wrote:
A method can refer to private attributes etc. in other objects than
self. This is unlike Smalltalk and like C++. Which objects? Obviously,
those that _have_ them in the first place.
Correct, though those classes also has to trust the calling class:
class MyClass { has $!attr; trusts YourClass }
class YourClass {
method foo (MyClass $obj) {
$obj!MyClass::attr; # Access to $obj's private attr
}
}
So, do classes trust their derived classes by default? Is trust itself
inherited?
No, there is no implicit trust, even of subclasses. For if that were the
case, you would have a couple of problems at least:
1. A major advantage of having things private is that it lets subclasses
not have to worry about implementation details of parent classes.
Subclasses can declare their own private or public attributes or methods
with the same names as private attributes or methods of their parents, and
they won't conflict. If the parent adds new privates later, it doesn't
have to worry about conflict with any subclasses. By contrast, typical
(hash-based) Perl 5 classes have to deal with this problem all the time as
all attributes and methods are actually public even if undocumented.
2. It would be very easy for anyone to defeat the security that the privacy
is supposed to give, without the class being exposed having any say about it.
-- Darren Duncan