Skip to content

Commit

Permalink
Example for submethod BUILD
Browse files Browse the repository at this point in the history
  • Loading branch information
moritz committed Mar 10, 2015
1 parent 65c7cab commit e721247
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion lib/Language/objects.pod
Expand Up @@ -267,7 +267,40 @@ Submethods are useful for object construction and destruction tasks, as well
as for tasks that are so specific to a certain type that subtypes must
certainly override them.
TODO: example
For example the L<default method new|/type/Mu#method new> calls submethod
C<BUILD> on each class in an L<inheritance|#Inheritance> chain:
=begin code
class Point2D {
has $.x;
has $.y;
submethod BUILD(:$!x, :$!y) {
say "Initializing Point2D";
}
}
class InvertiblePoint2D is Point2D {
submethod BUILD() {
say "Initializing InvertiblePoint2D";
}
method invert {
self.new(x => - $.x, y => - $.y);
}
}
say InvertiblePoint2D.new(x => 1, y => 2);
=end code
This produces the following output:
=begin code
Initializing Point2D
Initializing InvertiblePoint2D
InvertiblePoint2D.new(x => 1, y => 2)
=end code
See also: L<#Object Construction>.
=head2 Inheritance
Expand Down

0 comments on commit e721247

Please sign in to comment.