Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Some initial documentation for TWEAK
  • Loading branch information
moritz committed Nov 9, 2016
1 parent 2830ab9 commit 7f7977d
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion doc/Language/objects.pod6
Expand Up @@ -442,6 +442,10 @@ name. In either case, if neither C<BUILD> nor the default mechanism has
initialized the attribute, default values are applied (the C<2 * $!x> in the
example above).
After the C<BUILD> methods have been called, methods named C<TWEAK> are
called, if they exist, again with all the named arguments that were passed
to C<new>.
Due to the nature of the default behavior of C<BUILDALL> and custom C<BUILD>
submethods, named arguments to the constructor C<new> derived from C<Mu> can
correspond directly to public attributes of any of the classes in the method
Expand Down Expand Up @@ -521,7 +525,21 @@ class MyClass {
}
=end code
=comment TODO: better example for BUILDALL overriding
If all you need to do is checking things after the actual construction,
or modify attributes after the object construction, writing a submethod
C<TWEAK> is a good approach:
=begin code
class RectangleWithCachedArea {
has ($.x1, $.x2, $.y1, $.y2);
has $.area;
submethod TWEAK() {
$!area = abs( ($!x2 - $!x1) * ( $!y2 - $!y1) );
}
}
say RectangleWithCachedArea.new( x2 => 5, x1 => 1, y2 => 1, y1 => 0).area;
=end code
=head2 Object Cloning
Expand Down

0 comments on commit 7f7977d

Please sign in to comment.