|Perl Monk, Perl Meditation|
Perl 6 property mini-tutorialby TheDamian (Priest)
|on Aug 20, 2003 at 20:59 UTC||Need Help??|
The is keyword tells a declaration that it has certain properties. The behaviour of each of those properties is specified as a distinct class (hereafter called a "property class"). A property class has certain special methods (hereafter called "property methods") that determine what effect the property has on the referent to which it is applied.
So, for example, a built-in property class like class dim has property methods that modify the dimensionality of the underlying Array referent that an is dim property is applied to.
Whereas a built-in property class like class constant has property methods that modify the writeability of the referent that the property is applied to.
Similarly, a user-defined class such as class Persistent might have user-defined property methods that modify the persistence behaviour of any referent to which the is Persistent property is applied.
If a user-defined class (say, class Foo) doesn't have explicit property methods, it automatically has implicit property methods (which it inherits from the Class meta-class). Those inherited methods cause class Foo to modify -- in one of two ways -- any referent to which the is Foo property is applied.
If is Foo is applied to a class (say Bar), class Foo's property methods add Foo to the list of class Bar's ancestors. Thus:
is how we do inheritance.
On the other hand, if is Foo is applied to a non-type (say $bar), Foo's property methods replace the implementation type of $bar (i.e. Scalar) with the implementation type Foo. Thus:
is how we do tying.
So this one property mechanism gives us a wide range of useful language features (i.e. type qualification, referent modification, inheritance, tying) from a single underlying behaviour (namely, apply certain methods of this property class to the referent of this declaration).