Skip to content

Commit ff25568

Browse files
committed
Expands explanation on is rw trait
With new examples, and a reference to `is rw` on the whole class. It was probably better where it is, in classtut, rather than in the typesystem, which didn't seem to have (at least now, 18 months after OP) a place for it. This closes #1572
1 parent 39b1b74 commit ff25568

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

doc/Language/classtut.pod6

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,16 +257,15 @@ without having to write the method by hand. You are free instead to write
257257
your own accessor method, if at some future point you need to do something
258258
more complex than return the value.
259259
260+
X<|traits,is rw>
260261
Note that using the C<.> twigil has created a method that will provide
261262
read-only access to the attribute. If instead the users of this object
262263
should be able to reset a task's completion state (perhaps to perform it
263264
again), you can change the attribute declaration:
264265
265266
has Bool $.done is rw;
266267
267-
X<|traits,is rw>
268-
269-
The C<is rw> trait causes the generated accessor method to return something
268+
The C<is rw> trait causes the generated accessor method to return a container
270269
external code can modify to change the value of the attribute.
271270
272271
You can also supply default values to attributes (which works equally for
@@ -281,6 +280,19 @@ evaluated at that time, and can even reference earlier attributes:
281280
has Task @!dependencies;
282281
has $.ready = not @!dependencies;
283282
283+
Writable attributes are accessible through writable containers:
284+
285+
=for code
286+
class a-class {
287+
has $.an-attribute is rw;
288+
}
289+
say (a-class.new.an-attribute = "hey"); # OUTPUT: «hey␤»
290+
291+
This attribute can also be accessed using the C<.an-attribute> or
292+
C<.an-attribute()> syntax. See also
293+
L<the C<is rw> trait on classes|/language/typesystem#trait_is_rw> for examples
294+
on how this works on the whole class.
295+
284296
X<|class variables>
285297
A class declaration can also include I<class variables>, which are variables
286298
that are shared by all instances, and can be used for things like counting the

0 commit comments

Comments
 (0)