Skip to content

Commit 1bab6f4

Browse files
authored
Expand on the use of {*} with proto (#3500)
* Expand on the use of {*} with proto The documentation was not as clear as I needed. I tried several times to understand it, but failed. I got it nearly right when I asked on perl6-users. Crucially '{*}' is a special token without whitespace - this needs to be noted. The extra example combines the proto declaration with methods that use it. * inside the curly braces - edit * add example classname 'newclass'
1 parent 09616a5 commit 1bab6f4

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

doc/Language/functions.pod6

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,8 @@ because C<42> doesn't match C<Str>.
417417
=for code :preamble<sub congratulate {}>
418418
say &congratulate.signature # OUTPUT: «(Str $reason, Str $name, | is raw)␤»
419419
420-
You can give the C<proto> a function body, and place the C<{*}> where
420+
You can give the C<proto> a function body, and place the C<{*}> (note there is no
421+
whitespace inside the curly braces) where
421422
you want the dispatch to be done. This can be useful when you have a
422423
"hole" in your routine that gives it different behavior depending on the
423424
arguments given:
@@ -463,6 +464,49 @@ mistake-proto(7, 42); # OUTPUT: «Int␤» -- not passed on
463464
=for code :skip-test<illustrates error>
464465
mistake-proto('test'); # fails -- not passed on
465466
467+
A longer example using C<proto> for methods shows how to extract common functionality into
468+
a proto method.
469+
470+
=for code
471+
class NewClass {
472+
has $.debug is rw = False;
473+
has $.value is rw = 'Initial value';
474+
proto method handle( | ) {
475+
note "before value is 「$.value」" if $.debug;
476+
{*}
477+
note "after value is 「$.value」" if $.debug;
478+
}
479+
multi method handle(Str $s) {
480+
$.value = $s;
481+
say 'in string'
482+
}
483+
multi method handle(Positional $s) {
484+
$.value = $s[0];
485+
say 'in positional'
486+
}
487+
multi method handle( $a, $b ) {
488+
$.value = "$a is looking askance at $b";
489+
say 'in string'
490+
}
491+
}
492+
my NewClass $x .= new;
493+
$x.handle('hello world');
494+
$x.handle(<hello world>);
495+
$x.debug = True;
496+
$x.handle('hello world');
497+
$x.handle(<hello world>);
498+
$x.handle('Claire', 'John');
499+
# OUTPUT:
500+
#in string
501+
#after value is 「hello world」
502+
#before value is 「hello world」
503+
#in positional
504+
#after value is 「hello」
505+
#before value is 「hello」
506+
#in string
507+
#after value is 「Claire is looking askance at John」
508+
509+
466510
=head2 X<only|declarator>
467511
468512
The C<only> keyword preceding C<sub> or C<method> indicates that it will be the

xt/code.pws

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ nativeendian
413413
nativesize
414414
nbr
415415
nbsp
416+
newclass
416417
newfunc
417418
nextone
418419
niler

0 commit comments

Comments
 (0)