Skip to content

Commit b1bfd97

Browse files
committed
Elaborate on the "my Foo .= new()" idiom
1 parent dbdd6ac commit b1bfd97

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

doc/Language/5to6-perlop.pod6

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,24 @@ $x = $ok ?? $yes !! $no; # Perl 6
204204
Although not fully documented, S03 indicates that the mathematical and
205205
logical assignment operators should work as you would expect. The one
206206
noticeable change is that C<.=> calls a mutating method on the object on
207-
the left, while C<~=> is the string concatenation assignment, as you
208-
might expect with the changes in C<.> and C<~>. Also, the bitwise
209-
assignment operators are likely not separated into numeric and string
210-
versions (C<&=>, etc., vs. C<&.=>, etc.), as that feature is currently
211-
experimental in Perl 5 itself - although, again, this is not
212-
specifically documented.
207+
the left (which can also be a type-object). This allows for the following
208+
useful idiom:
209+
210+
=begin code
211+
class LongClassName {
212+
has $.frobnicate;
213+
}
214+
my LongClassName $bar .= new( frobnicate => 42 ); # no need to repeat class name
215+
=end code
216+
217+
This ensures that C<$bar> will only be able to contain a C<LongClassName>
218+
object, as well not having to repeat (and possibly misspell) the class name.
219+
220+
C<~=> is the string concatenation assignment, as you might expect with the
221+
changes in C<.> and C<~>. Also, the bitwise assignment operators are likely
222+
not separated into numeric and string versions (C<&=>, etc., vs. C<&.=>, etc.),
223+
as that feature is currently experimental in Perl 5 itself - although, again,
224+
this is not specifically documented.
213225
214226
=head2 Comma Operator
215227

0 commit comments

Comments
 (0)