@@ -53,6 +53,10 @@ argument of C<is default> will set the default item value or hash value.
53
53
54
54
= head2 X < Trait is required|trait,is required (Attribute) >
55
55
56
+ Defined as:
57
+
58
+ multi sub trait_mod:<is> (Attribute $attr, :$required!)
59
+
56
60
The trait C < is required > will mark the attribute as to be filled with a value
57
61
when the object is instantiated. Failing to do so will result in a runtime
58
62
error.
@@ -76,6 +80,10 @@ CATCH{ default { say .^name, ': ', .Str } }
76
80
# OUTPUT: «X::Attribute::Required: The attribute '$!a' is required because it is a good idea,but you did not provide a value for it.»
77
81
= end code
78
82
83
+ C < is required > doesn't just affect the default constructor, it checks for the
84
+ attribute at a lower level, so it will work for custom constructors written
85
+ using L < bless|/routine/bless > .
86
+
79
87
= head2 X < trait is DEPRECATED|trait,is DEPRECATED (Attribute) >
80
88
81
89
multi sub trait_mod:<is>(Attribute:D $r, :$DEPRECATED!)
@@ -97,6 +105,26 @@ STDERR:
97
105
# script.p6, line 5
98
106
# Please use 'bar' instead.
99
107
108
+ = head2 X < trait is rw|trait,is rw (Attribute) >
109
+
110
+ Defined as:
111
+
112
+ multi sub trait_mod:<is> (Attribute:D $attr, :$rw!)
113
+
114
+ Marks an attribute as read/write as opposed to the default C < readonly > . The
115
+ default accessor for the attribute will return a writable value.
116
+
117
+ class Boo {
118
+ has $.bar is rw;
119
+ has $.baz;
120
+ };
121
+
122
+ my $boo = Boo.new;
123
+ $boo.bar = 42; # works
124
+ $boo.baz = 42;
125
+ CATCH { default { put .^name, ': ', .Str } };
126
+ # OUTPUT: «X::Assignment::RO: Cannot modify an immutable Any»
127
+
100
128
= head1 Methods
101
129
102
130
= head2 method name
@@ -220,50 +248,6 @@ Binds the value C<new_val> to this attribute of object C<$obj>.
220
248
Note that this method violates encapsulation of the object, and should be
221
249
used with care. Here be dragons.
222
250
223
- = head1 Traits
224
-
225
- = head2 trait is required
226
-
227
- multi sub trait_mod:<is> (Attribute $attr, :$required!)
228
-
229
- Marks an attribute as being required. If a value is not provided
230
- during object construction, an exception is thrown.
231
-
232
- class Foo {
233
- has $.bar is required;
234
- has $.baz;
235
- };
236
-
237
- Foo.new(bar => 42); # works
238
- Foo.new(baz => 42);
239
- CATCH { default { put .^name, ': ', .Str } };
240
- # OUTPUT: «X::Attribute::Required: The attribute '$!bar' is required, but you did not provide a value for it.»
241
-
242
- C < is required > doesn't just affect the default constructor, it checks for the
243
- attribute at a lower level, so it will work for custom constructors written
244
- using L < bless|/routine/bless > .
245
-
246
-
247
- = head2 trait is rw
248
-
249
- Defined as:
250
-
251
- multi sub trait_mod:<is> (Attribute:D $attr, :$rw!)
252
-
253
- Marks an attribute as read/write as opposed to the default C < readonly > . The
254
- default accessor for the attribute will return a writable value.
255
-
256
- class Boo {
257
- has $.bar is rw;
258
- has $.baz;
259
- };
260
-
261
- my $boo = Boo.new;
262
- $boo.bar = 42; # works
263
- $boo.baz = 42;
264
- CATCH { default { put .^name, ': ', .Str } };
265
- # OUTPUT: «X::Assignment::RO: Cannot modify an immutable Any»
266
-
267
251
= head2 method gist
268
252
269
253
Defined as
0 commit comments