John icked:
   
   > > #     # Change apple color, but leave the blue berry
   > > #     $obj.fruit(apple => 'green');
   > > #
   > > #     print $obj.fruit('apple'); # green
   > > 
   > > $obj.fruit{apple}
   > 
   > Icky, looks "unencapsulated" to me :)

[For the benefit of those playing along at home, the smiley there
 indicates that John knows better.

 The fruit() method would only be auto-generated if the $.fruit
 attribute had been marked C<is public>:

        class Fruitbowl;

        my %.fruit is public;
 
 So, at that level, it is indeed unencapsulated. Deliberately. By design.

 However, since access to the $.fruit attribute is *mediated*
 by the call to the fruit() method, it really is still encapsulated.
 Specifically, the direct, unrestricted, dangerous access it currently
 provides could later be indirected, restricted, and made safe without
 changing any client code. We would just rewrite the class as:

        class FruitBowl;

        my %.fruit;             # default is "private"

        method fruit () {
                return Proxy::Object->new(\%.fruit);
        }

 Now those "unencapsulated" accesses to $obj.fruit{apple} would
 be safely "encapsulated" behind calls to Proxy::Object::FETCH and
 Proxy::Object::STORE.

 We now return you to the regular mayhem of p6-lang...
]

Reply via email to