|
| 1 | +=begin pod |
| 2 | +
|
| 3 | +=TITLE class Attribute |
| 4 | +
|
| 5 | + class Attribute { } |
| 6 | +
|
| 7 | +In Perl 6 lingo, an I<attribute> refers to a per-instance/object storage slot. |
| 8 | +An C<Attribute> is used to talk about classes' and roles' attributes on the |
| 9 | +meta level. |
| 10 | +
|
| 11 | +Normal usage of attributes does not require the user to use class Attribute |
| 12 | +explicitly. |
| 13 | +
|
| 14 | +The usual way to obtain an object of type C<Attribute> is by introspection: |
| 15 | +
|
| 16 | + class Useless { |
| 17 | + has @!things; |
| 18 | + } |
| 19 | + my $a = Useless.^attributes(:local)[0]; |
| 20 | + say $a.name; # @!things |
| 21 | + say $a.package; # Useless() |
| 22 | + say $a.has-accessor; # False |
| 23 | +
|
| 24 | + # modifying an attribute from the outside |
| 25 | + # this is usually not possible, but since Attribute |
| 26 | + # is at the level of the meta class, all is fair game |
| 27 | + my $instance = Useless.new; |
| 28 | + $a.set_value($instance, [1, 2, 3]); |
| 29 | + say $a.get_value($instance); # 1 2 3 |
| 30 | +
|
| 31 | +=head1 Methods |
| 32 | +
|
| 33 | +=head2 name |
| 34 | +
|
| 35 | + method name(Attribute:D:) returns Str:D |
| 36 | +
|
| 37 | +Returns the name of the attribute. Note that this is always the private name, |
| 38 | +so if an attribute is declared as C<has $.a>, the name returned is C<$!a>. |
| 39 | +
|
| 40 | +=head2 package |
| 41 | +
|
| 42 | + method package(Attribute:D:) returns Mu:U |
| 43 | +
|
| 44 | +Returns the package (class/grammar/role) to which this attribute belongs. |
| 45 | +
|
| 46 | +=head2 has-accessor |
| 47 | +
|
| 48 | + method has-accessor(Attribute:D:) returns Bool:D |
| 49 | +
|
| 50 | +Returns C<True> if the attribute has a public accessor method. |
| 51 | +
|
| 52 | +=head2 readonly |
| 53 | +
|
| 54 | + method readonly(Attribute:D:) returns Bool:D |
| 55 | +
|
| 56 | +Returns C<True> for readonly attributes, which is the default. |
| 57 | +Returns C<False> for attributes marked as C<is rw>. |
| 58 | +
|
| 59 | +=head2 get_value |
| 60 | +
|
| 61 | + method get_value(Attribute:D: Mu $instance) |
| 62 | +
|
| 63 | +Returns the value stored in this attribute of object C<$instance>. |
| 64 | +
|
| 65 | +Note that this method violates encapsulation of the object, and should be |
| 66 | +used with care. Here be dragons. |
| 67 | +
|
| 68 | +=head2 set_value |
| 69 | +
|
| 70 | + method set_value(Attribute:D: Mu $instance, Mu \new_val) |
| 71 | +
|
| 72 | +Binds the value C<new_val> to this attribute of object C<$instance>. |
| 73 | +
|
| 74 | +Note that this method violates encapsulation of the object, and should be |
| 75 | +used with care. Here be dragons. |
| 76 | +
|
| 77 | +=end pod |
0 commit comments