Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Perl 6 property mini-tutorial

by TheDamian (Priest)
on Aug 20, 2003 at 20:59 UTC ( #285293=note: print w/ replies, xml ) Need Help??


in reply to Perl 6 'is dim' point

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:

class Bar is Foo {...}
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:

my $bar is Foo;
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).


Comment on Perl 6 property mini-tutorial
Select or Download Code
Re: Perl 6 property mini-tutorial
by John M. Dlugosz (Monsignor) on Aug 28, 2003 at 17:56 UTC
    Thanks for the additional information.

    I suppose the "property methods" basically is a function that is given the thing to which it is applying, and it can monkey with it any way it wants.

    —John

      Yes, exactly. Each property method is a method (i.e. an class-specific function) that can be set up to do a particular kind of monkeying with the referent to which the original property was applied.

      The two main advantages of encapsulating these functions in classes are that anyone who is defining a property can provide state (i.e. class attributes) for that property if necessary, and can also create sets of related properties using inheritance.

      A more subtle advantage is that using a class-based interface for specifying properties gives us a clean way to extend that interface at a later time, should we miss some useful feature in the initial design.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://285293]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2014-09-20 09:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (158 votes), past polls