Skip to content

Commit 9b7993b

Browse files
committed
Adds TWEAK examples, closes #1393
1 parent 509428b commit 9b7993b

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

doc/Language/classtut.pod6

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ say $r.area(); # OUTPUT: «100␤»
3535
In the two classes, the default constructor is being used. This constructor will
3636
use named parameters in its invocation: C<Point.new(x => 0, y => 0)>.
3737
38+
X<submethods>
3839
You can also provide your own construction and C<BUILD> implementation. The
3940
following, more elaborate example shows how a dependency handler might look in
4041
Perl 6. It showcases custom constructors, private and public attributes,
@@ -95,6 +96,30 @@ parameters. That is one of the reasons why using C<new> is discouraged. If you
9596
need to declare it anyway, use C<multi method new> if you do not want to disable
9697
the default constructors.
9798
99+
C<TWEAK> is the last submethod to be called, and it has got the advantage of
100+
having the object properties available without needing to use the meta object
101+
protocol. It can be used, for instance, to assign values to instance variables
102+
based on the values of other attributes or instance variables:
103+
104+
=begin code
105+
class Str-with-ID is Str {
106+
my $.counter = 0;
107+
has Str $.string;
108+
has Int $.ID;
109+
110+
method TWEAK() {
111+
$!ID = $.counter++;
112+
}
113+
}
114+
115+
say Str-with-ID.new(string => 'First').ID; # OUTPUT: «0»
116+
say Str-with-ID.new(string => 'Second').ID; # OUTPUT: «1»
117+
=end code
118+
119+
In this case, we need to compute C<$.ID> from the value of a counter that is a
120+
class variable, C<$.counter>, thus we simply assign a value to it and increment
121+
the counter at the same time.
122+
98123
=head1 Starting with class
99124
100125
X<|class>
@@ -239,7 +264,7 @@ class Str-with-ID is Str {
239264
has Int $.ID;
240265
241266
method new( Str $string ) {
242-
self.bless( $:string, ID => $.counter++ )
267+
self.bless( :$string, ID => $.counter++ )
243268
}
244269
}
245270
@@ -250,7 +275,10 @@ say Str-with-ID.new('Second').ID; # OUTPUT: «1»
250275
In this case, using C<new> might be the easiest way to initialize the C<$.ID>
251276
field and increment the value of the counter at the same time. C<new>, through
252277
C<bless>, will invoke the default C<BUILD>, assigninng the values to their
253-
properties correctly.
278+
properties correctly. You can obtain the same effect using C<TWEAK>, which is
279+
considered a more I<p6y> way. Please check <the section on submethods|/language/classtut#index-entry-OOP>
280+
for an alternative example on how
281+
to do this.
254282
255283
=head1 Static fields?
256284

doc/Language/objects.pod6

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -630,21 +630,21 @@ need to C<callsame> to invoke the parent classes C<BUILDALL>.
630630
}
631631
=end code
632632
633-
The C<TWEAK> method allows you to check things or modify attributes after
633+
The C<TWEAK> submethod allows you to check things or modify attributes after
634634
object construction:
635635
636-
=begin code
637-
class RectangleWithCachedArea {
638-
has ($.x1, $.x2, $.y1, $.y2);
639-
has $.area;
640-
submethod TWEAK() {
641-
$!area = abs( ($!x2 - $!x1) * ( $!y2 - $!y1) );
642-
}
636+
=begin code
637+
class RectangleWithCachedArea {
638+
has ($.x1, $.x2, $.y1, $.y2);
639+
has $.area;
640+
submethod TWEAK() {
641+
$!area = abs( ($!x2 - $!x1) * ( $!y2 - $!y1) );
643642
}
643+
}
644644
645-
say RectangleWithCachedArea.new( x2 => 5, x1 => 1, y2 => 1, y1 => 0).area;
646-
# OUTPUT: «4␤»
647-
=end code
645+
say RectangleWithCachedArea.new( x2 => 5, x1 => 1, y2 => 1, y1 => 0).area;
646+
# OUTPUT: «4␤»
647+
=end code
648648
649649
=head2 Object Cloning
650650

0 commit comments

Comments
 (0)