Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Mention build-time attribute defaults in classtut.
Closes #79
  • Loading branch information
moritz committed May 9, 2015
1 parent a8dbc9a commit 0c94ad7
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lib/Language/classtut.pod
Expand Up @@ -166,6 +166,17 @@ X<|traits, is rw>
The C<is rw> trait causes the generated accessor method to return something
external code can modify to change the value of the attribute.
You can also supply default values to attributes (which works equally for
those with and without accessors):
has Bool $.done = False;
The assignment is carried out at object build time. The right-hand side is
evaluated at that time, and can even reference earlier attributes:
has Task @!dependencies;
has $.ready = not @!dependencies;
=head1 Static fields?
Perl 6 has no B<static> keyword. Nevertheless any class may declare anything
Expand Down Expand Up @@ -304,6 +315,22 @@ as the bind targets for C<BUILD>'s parameters. Zero-boilerplate
initialization! See L<objects|/language/objects#Object Construction> for
more information.
The C<BUILD> method is responsible for initialilzing all attributes, it must
handle default values too:
has &!callback;
has @!dependencies;
has Bool ($.done, $.ready);
submethod BUILD(
:@!callback,
:@!dependencies,
:$!done = False,
:$!ready = not @!dependencies
) { }
See L<Object Construction|/language/objects#Object Construction> for more
options to influence object construction and attribute initializtion.
=head1 Consuming our class
After creating a class, you can create instances of the class. Declaring a
Expand Down

0 comments on commit 0c94ad7

Please sign in to comment.