While thinking Eiffel-ish thoughts the other day, I began to wonder if
Perl6's classes could go beyond the simple private/public/protected
scheme by optionally allowing for a property or method to only be
accessed by a certain set of classes. For instance(as I understand
Perl6 syntax):
class Foo {
method hello is public {
return "hello";
}
method world is public_to(Bar) {
# is only public to objects of class Bar
return "world";
}
}
class Bar {
method say_hello {
# this works fine
print Foo.new.hello, "\n";
}
method say_world {
# as does this
print Foo.new.world, "\n";
}
}
class Baz {
method say_world {
# generates a run-time exception for trying to call a private method.
print Foo.new.world, "\n";
}
}