@@ -13,9 +13,9 @@ you need to pass to the code or function in order to call it.
13
13
Passing arguments to a signature I < binds > the arguments, contained in
14
14
a L < Capture > , to the signature.
15
15
16
- X < |signature literal (Signature) >
17
- X < |:() (Signature) >
18
- = head1 Signature Literals
16
+ X < |signature literal >
17
+ X < |:() >
18
+ = head1 Signature literals
19
19
20
20
Signatures appear inside parentheses after L < subroutine|/type/Sub > and
21
21
L < method|/type/Method > names, on blocks after a C << -> >> or C << <-> >> arrow,
@@ -79,7 +79,7 @@ keys of the Hash.
79
79
say %h ~~ :(:$left, :$right);
80
80
# OUTPUT: «True»
81
81
82
- = head2 Parameter Separators
82
+ = head2 Parameter separators
83
83
84
84
A signature consists of zero or more I < L < parameters|Parameter > > , separated by
85
85
commas.
@@ -102,7 +102,7 @@ is bound to.
102
102
}
103
103
say Foo.whoami; # OUTPUT: «Well I'm class Foo, of course!»
104
104
105
- X < |type constraint (Signature) >
105
+ X < |type constraint >
106
106
X < |Constraint >
107
107
= head2 Type constraints
108
108
@@ -128,9 +128,9 @@ subset is based, in this case C<Int>. If it fails, a C<Type check> error is
128
128
produced. Once that filter is cleared, the constraint that defined the subset is
129
129
checked, producing a C < I < Constraint > type check> error if it fails.
130
130
131
- X < |anonymous arguments (Signature) > Anonymous arguments are fine too, if you
132
- don't actually need to refer to a parameter by name, for instance to distinguish
133
- between different signatures in a
131
+ X < |anonymous arguments >
132
+ Anonymous arguments are fine too, if you don't actually need to refer to a
133
+ parameter by name, for instance to distinguish between different signatures in a
134
134
L < multi|/language/functions#index-entry-declarator_multi-Multi-dispatch > or to
135
135
check the signature of a L < Callable > .
136
136
@@ -140,7 +140,7 @@ check the signature of a L<Callable>.
140
140
141
141
Type constraints may also be L < type captures|/type/Signature#Type_Captures > .
142
142
143
- X < |where clause (Signature) >
143
+ X < |where clause >
144
144
In addition to those I < nominal > types, additional constraints can
145
145
be placed on parameters in the form of code blocks which must return
146
146
a true value to pass the type check
@@ -193,7 +193,7 @@ the C<where>-clause inside the sub-signature.
193
193
# foo 2, 3;
194
194
# OUTPUT: «Constraint type check failed in binding to parameter '$b'…»
195
195
196
- = head3 Constraining Optional Arguments
196
+ = head3 Constraining optional arguments
197
197
198
198
L < Optional arguments|#Optional_and_Mandatory_Parameters > can have constraints,
199
199
too. Any C < where > clause on any parameter will be executed, even if it's
@@ -202,7 +202,7 @@ against undefined values within the C<where> clause.
202
202
203
203
sub f(Int $a, UInt $i? where { !$i.defined or $i > 5 } ) { ... }
204
204
205
- = head3 Constraining Slurpy Arguments
205
+ = head3 Constraining slurpy arguments
206
206
207
207
L < Slurpy arguments|#Slurpy_(A.K.A._Variadic)_Parameters > can not have type
208
208
constraints. A C < where > -clause in conjunction with a L < Junction|/type/Junction >
@@ -214,7 +214,7 @@ can be used to that effect.
214
214
CATCH { default { say .^name, ' ==> ', .Str } }
215
215
# OUTPUT: «[42]Constraint type check failed in binding to parameter '@a' ...»
216
216
217
- = head3 Constraining named Arguments
217
+ = head3 Constraining named arguments
218
218
219
219
Constraints against L < Named arguments|#Positional_vs._Named > apply to the value
220
220
part of the L < colon-pair|/type/Pair > .
@@ -225,8 +225,11 @@ part of the L<colon-pair|/type/Pair>.
225
225
# OUTPUT: «X::TypeCheck::Binding::Parameter ==> Type check failed in
226
226
# binding to parameter '$i'; expected Int but got Str ("forty-two")»
227
227
228
- = head3 X < Constraining Defined and Undefined Values|
229
- type constraint,:D;type constraint,:U;type constraint,:_>
228
+ |
229
+ X < |type constraint,:D >
230
+ X < |type constraint,:U >
231
+ X < |type constraint,:_ >
232
+ = head3 Constraining defined and undefined values
230
233
231
234
Normally, a type constraint only checks whether the value of the parameter is of
232
235
the correct type. Crucially, both I < object instances > and I < type objects > will
@@ -254,10 +257,10 @@ and type objects (C<Int>). Consider the following code:
254
257
# (Str:D $: *%_)»
255
258
say limit-lines "a \n b", Int # Always returns the max number of lines
256
259
257
- Here we really only want to deal with string instances, not
258
- type objects. To do this, we can use the C < :D > type constraint. This constraint
259
- checks that the value passed is an I < object instance > , in a similar fashion to calling
260
- its L < DEFINITE|/language/mop#DEFINITE > method.
260
+ Here we really only want to deal with string instances, not type objects. To do
261
+ this, we can use the C < :D > type constraint. This constraint checks that the
262
+ value passed is an I < object instance > , in a similar fashion to calling its
263
+ L < DEFINITE|/language/mop#DEFINITE > method.
261
264
262
265
To warm up, let's apply C < :D > to the right-hand side of our humble C < Int > example:
263
266
@@ -349,7 +352,7 @@ a type object, which would fail the definiteness constraint.
349
352
sub divide (Int:D :$a = 2, Int:D :$b!) { say $a/$b }
350
353
divide :1a, :2b; # OUTPUT: «0.5»
351
354
352
- = head3 X « Constraining signatures of Callables| Callable (constrain) »
355
+ = head3 Constraining signatures of C < Callable > s
353
356
354
357
A L < Callable > parameter can be constrained by its signature, by specifying
355
358
a L < Signature > literal right after the parameter (no whitespace allowed):
@@ -368,14 +371,15 @@ others, you need to use the long version:
368
371
# f(&g); # Constraint type check failed
369
372
f(&h); # OUTPUT: «ten10»
370
373
371
- = head3 X « Constraining Return Types|-->;returns »
374
+ = head3 Constraining return types
372
375
373
- There are multiple ways to constrain return types on a L < Routine|/type/Routine > . All versions below
374
- are currently valid and will force a type check on successful execution of a routine.
376
+ There are multiple ways to constrain return types on a L < Routine|/type/Routine > .
377
+ All versions below are currently valid and will force a type check on successful
378
+ execution of a routine.
375
379
376
- L < C < Nil > |/type/Nil> and L < C < Failure > |/type/Failure> are always allowed as return types,
377
- regardless of any type constraint. This allows L < Failure|/type/Failure > to be returned
378
- and passed on down the call chain.
380
+ L < C < Nil > |/type/Nil> and L < C < Failure > |/type/Failure> are always allowed as return
381
+ types, regardless of any type constraint. This allows L < Failure|/type/Failure >
382
+ to be returned and passed on down the call chain.
379
383
380
384
sub foo(--> Int) { Nil };
381
385
say foo.perl; # OUTPUT: «Nil»
@@ -449,7 +453,7 @@ except the C<$var> is a definition for a routine.
449
453
= for code :skip-test
450
454
my 42 sub bad-answer {}; # This will fail.
451
455
452
- = head3 X < Coercion Type|coercion type (signature) >
456
+ = head3 X < Coercion type >
453
457
454
458
To accept one type but coerce it automatically to another, use the
455
459
accepted type as an argument to the target type. If the accepted type is
@@ -489,9 +493,13 @@ for 2,4, *² … 256 -> $a {
489
493
# 256² is 5 figures long»
490
494
= end code
491
495
492
- In this example, coercing the return type to C < String > allows us to directly apply string methods, such as the number of characters.
496
+ In this example, coercing the return type to C < String > allows us to directly
497
+ apply string methods, such as the number of characters.
493
498
494
- = head2 X < Slurpy (A.K.A. Variadic) Parameters|parameter,*@;parameter,*%,slurpy argument (Signature) >
499
+ X < |parameter,*@ >
500
+ X < |parameter,*% >
501
+ X < |slurpy argument >
502
+ = head2 Slurpy (A.K.A. variadic) parameters
495
503
496
504
A function is X < variadic > if it can take a varying number of arguments; that is,
497
505
its arity is not fixed. Therefore, optional, named, and slurpy parameters are
@@ -535,7 +543,7 @@ Slurpy parameters have special behaviors when combined with some
535
543
L < traits and modifiers|#Parameter_Traits_and_Modifiers > ,
536
544
as described in L < the section on slurpy array parameters|/type/Signature#Types_of_Slurpy_Array_Parameters > .
537
545
538
- = head2 Types of Slurpy Array Parameters
546
+ = head2 Types of slurpy array parameters
539
547
540
548
There are three variations to slurpy array parameters.
541
549
@@ -549,7 +557,7 @@ Each will be described in detail in the next few sections. As the difference
549
557
between each is a bit nuanced, examples are provided for each to demonstrate how
550
558
each slurpy convention varies from the others.
551
559
552
- = head3 Flattened Slurpy
560
+ = head3 Flattened slurpy
553
561
554
562
Slurpy parameters declared with one asterisk will flatten arguments by
555
563
dissolving one or more layers of bare L < Iterable|/type/Iterable > s.
@@ -568,10 +576,11 @@ a(($_ for 1, 2, 3)); # OUTPUT: «[1, 2, 3]»
568
576
A single asterisk slurpy flattens all given iterables, effectively hoisting any
569
577
object created with commas up to the top level.
570
578
571
- = head3 X < Unflattened Slurpy |parameter, **@ >
579
+ = head3 X < Unflattened slurpy |parameter, **@ >
572
580
573
- Slurpy parameters declared with two stars do not flatten any L < Iterable|/type/Iterable > arguments
574
- within the list, but keep the arguments more or less as-is:
581
+ Slurpy parameters declared with two stars do not flatten any
582
+ L < Iterable|/type/Iterable > arguments within the list, but keep the arguments
583
+ more or less as-is:
575
584
576
585
= begin code
577
586
my @array = <a b c>;
@@ -587,14 +596,15 @@ b(($_ for 1, 2, 3)); # OUTPUT: «[(1, 2, 3),]»
587
596
The double asterisk slurpy hides the nested comma objects and leaves them as-is
588
597
in the slurpy array.
589
598
590
- = head3 Single Argument Rule Slurpy
591
- X < |+ (Single Argument Rule Slurpy) >
599
+ X < |+ (Single argument rule slurpy) >
600
+ = head3 Single argument rule slurpy
601
+
592
602
593
603
A slurpy parameter created using a plus engages the I < "single argument rule" > ,
594
604
which decides how to handle the slurpy argument based upon context. Basically,
595
- if only a single argument is passed and that argument is L < Iterable|/type/Iterable > , that argument
596
- is used to fill the slurpy parameter array. In any other case, C < +@ > works like
597
- C < **@ > .
605
+ if only a single argument is passed and that argument is
606
+ L < Iterable|/type/Iterable > , that argument is used to fill the slurpy parameter
607
+ array. In any other case, C < +@ > works like C < **@ > .
598
608
599
609
= begin code
600
610
my @array = <a b c>;
@@ -609,10 +619,10 @@ c(($_ for 1, 2, 3)); # OUTPUT: «[1, 2, 3]»
609
619
610
620
For additional discussion and examples, see L < Slurpy Conventions for Functions|/language/functions#Slurpy_Conventions > .
611
621
612
- = head2 Type Captures
613
- X < | Type Capture (signature) >
622
+ X < | Type capture >
623
+ = head2 Type captures
614
624
615
- Type Captures allow deferring the specification of a type constraint to the time
625
+ Type captures allow deferring the specification of a type constraint to the time
616
626
the function is called. They allow referring to a type both in the signature and
617
627
the function body.
618
628
@@ -630,9 +640,9 @@ the function body.
630
640
my &s = f(10, 2, Int.new / Int.new);
631
641
say s(2); # 10 / 2 * 2 == 10
632
642
633
- X < |positional argument (Signature) >
634
- X < |named argument (Signature) >
635
- = head2 Positional vs. Named Arguments
643
+ X < |positional argument >
644
+ X < |named argument >
645
+ = head2 Positional vs. named arguments
636
646
637
647
An argument can be I < positional > or I < named > . All arguments are positional,
638
648
except slurpy hash and arguments marked with a leading colon C < : > .
@@ -664,8 +674,8 @@ variable name:
664
674
sub named(:official($private)) { "Official business!" if $private }
665
675
named :official;
666
676
667
- X < |aliases (Signature) >
668
- = head2 Argument Aliases
677
+ X < |aliases >
678
+ = head2 Argument aliases
669
679
The L < colon-pair|/type/Pair > syntax can be used to provide aliases for arguments:
670
680
671
681
sub alias-named(:color(:$colour), :type(:class($kind))) {
@@ -709,8 +719,8 @@ before slipping.
709
719
say C.new(|%h.Map);
710
720
# OUTPUT: «C.new(x => 5, y => 20, z => [1, 2])»
711
721
712
- X < |optional argument (Signature) >
713
- = head2 Optional and Mandatory Parameters
722
+ X < |optional argument >
723
+ = head2 Optional and mandatory arguments
714
724
715
725
Positional parameters are mandatory by default, and can be made optional
716
726
with a default value or a trailing question mark:
@@ -719,7 +729,7 @@ with a default value or a trailing question mark:
719
729
$ = :($base = 10); # optional parameter, default value 10
720
730
$ = :(Int $x?); # optional parameter, default is the Int type object
721
731
722
- X < |mandatory named argument (Signature) >
732
+ X < |mandatory named argument >
723
733
Named parameters are optional by default, and can be made mandatory with a
724
734
trailing exclamation mark:
725
735
@@ -733,14 +743,14 @@ notionally) computed anew for each call
733
743
$ = :($goal, $accuracy = $goal / 100);
734
744
$ = :(:$excludes = ['.', '..']); # a new Array for every call
735
745
736
- = head2 Dynamic Variables
746
+ = head2 Dynamic variables
737
747
738
748
L < Dynamic variables|/language/variables#The_*_Twigil > are allowed in signatures
739
749
although they don't provide special behaviour because argument binding does
740
750
connect two scopes anyway.
741
751
742
- X < |destructuring arguments (Signature) >
743
- = head2 Destructuring Parameters
752
+ X < |destructuring arguments >
753
+ = head2 Destructuring arguments
744
754
745
755
Parameters can be followed by a sub-signature in parentheses, which will
746
756
destructure the argument given. The destructuring of a list is just its
@@ -787,8 +797,7 @@ name in parentheses.
787
797
foo(42, "answer");
788
798
# OUTPUT: «called with \(42, "answer")»
789
799
790
- X < |Long Names >
791
- = head2 Long Names
800
+ = head2 X < Long names >
792
801
793
802
To exclude certain parameters from being considered in multiple dispatch,
794
803
separate them with a double semi-colon.
@@ -797,7 +806,7 @@ separate them with a double semi-colon.
797
806
f(10, 'answer');
798
807
# OUTPUT: «10, answer, Any»
799
808
800
- = head2 X < Capture Parameters |parameter,| >
809
+ = head2 X < Capture parameters |parameter,| >
801
810
802
811
Prefixing a parameter with a vertical bar C < | > makes the parameter a
803
812
L < C < Capture > > , using up all the remaining positional and named
@@ -816,7 +825,7 @@ operator C<|>.
816
825
b(42, "answer");
817
826
# OUTPUT: «CaptureInt Str»
818
827
819
- = head2 Parameter Traits and Modifiers
828
+ = head2 Parameter traits and modifiers
820
829
821
830
By default, parameters are bound to their argument and marked as
822
831
read-only. One can change that with traits on the parameter.
0 commit comments