Skip to content

Commit dec7182

Browse files
committed
Some work on the glossary
As part of moving stuff from S99 in here
1 parent a468121 commit dec7182

File tree

1 file changed

+44
-6
lines changed

1 file changed

+44
-6
lines changed

doc/Language/glossary.pod

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Note that it is still allowed to have a name
2020
# anonymous, but knows its own name
2121
my $s = anon sub triple($x) { 3 * $x }
2222
say $s.name; # triple
23+
say triple(42); # Undeclared routine: triple
2324
2425
=head1 Adverb
2526
@@ -38,7 +39,10 @@ reason colon pair notation is also known as the adverbial pair form:
3839
:a(4) # Same as "a" => 4
3940
4041
Some other forms that use a colon in ways that have adverb-like semantics
41-
are called adverbial forms.
42+
are called adverbial forms. One special form starts with an integer value,
43+
followed by a name (for the key):
44+
45+
:20seconds # same as seconds => 20
4246
4347
=head1 Autothreading
4448
@@ -49,7 +53,7 @@ these calls is assembled in a junction of the same type as the original
4953
junction.
5054
5155
sub f($x) { 2 * $x };
52-
if f(1|2|3) == 4 {
56+
if f(1|2|3) == 4 { # same as f(1) == 4 || f(2) == 4 || f(3) == 4
5357
say 'success';
5458
}
5559
@@ -64,8 +68,8 @@ arguments into multiple calls to a function is called I<autothreading>.
6468
A colon pair is a shorthand syntax used to create or visually present
6569
a Pair object. The two most common forms are:
6670
67-
:a(4) # Same as "a" => 4, same as Pair.new(:key<a>,:value(4))
68-
:a<4> # Same as "a" => "4", same as Pair.new(:key<a>,:value<4>)
71+
:a(4) # Same as "a" => 4, same as Pair.new("a",4)
72+
:a<4> # Same as "a" => "4", same as Pair.new("a","4")
6973
7074
This is also known as the adverbial pair form. Note: when the part after
7175
the colon and before the brackets is not a legal identifier, other
@@ -81,6 +85,15 @@ does not need commas, or even spaces:
8185
8286
:a(4):c:!d:c # Same as a => 4, c => True, d => False, c => True
8387
88+
Finally, if there is a variable with the same name as an intended adverbial
89+
pair, you don't have to specify the name twice, but just specify the adverb
90+
with the appropriate sigil:
91+
92+
:$foo # same as foo => $foo
93+
:@bar # same as bar => @bar
94+
:%mapper # same as mapper => %mapper
95+
:&test # same as test => &test
96+
8497
=head1 Constraint
8598
8699
Constraints are a restriction placed on acceptable types for a parameter
@@ -89,11 +102,22 @@ following example, a constraint is used to make sure an error occurs if
89102
anyone ever calls a subroutine named C<abbreviate> with a string which is
90103
shorter than 10 characters:
91104
92-
sub abbreviate (Str $thing where { .chars >= 10 }) { ... }
105+
sub abbreviate(Str $thing where { .chars >= 10 }) { ... }
93106
94107
The C<Str> in the above example is also a constraint, but is usually
95108
referred to as a "type constraint."
96109
110+
Note that you can elso differentiate candidates in a C<multi-dispatch>
111+
by using a different constraint:
112+
113+
multi sub abbreviate(Str $thing where { .chars >= 10 }) {
114+
"$thing.substr(0,10)..."
115+
}
116+
multi sub abbreviate(Str $thing) { $thing } # no constraint
117+
118+
say abbreviate("Worthington"; # Worthingto...
119+
say abbreviate("Mäsak"); # Mäsak
120+
97121
=head1 Instance
98122
99123
An I<instance> of a class is also called an I<object> in some other
@@ -129,13 +153,17 @@ in Perl 6. It is what C<self> refers to in a method.
129153
130154
say 'str'.uc; # 'str' is the invocant of method uc
131155
156+
class A { method show { self } }
157+
say A.new.show; # A.new
158+
132159
=head1 Literal
133160
134161
A I<literal> is a piece of code that directly stands for an (often built-in)
135162
object and also refers to the object itself.
136163
137164
my $x = 2; # the 2 is a literal
138165
say $x; # $x is not a literal, but a variable
166+
my $s = "Foo"; # the "Foo" is a literal, the $s is a variable
139167
140168
=head1 lvalue
141169
@@ -174,16 +202,26 @@ The C<mainline> is the program text that is not part of any kind of block.
174202
}
175203
f(); # in mainline again
176204
205+
You can also have the mainline of any package-like declarator, such as
206+
C<class>, C<module>, C<grammar>, etc. These are typically run just after
207+
the class/module/grammar have been compiled (or when loaded from a
208+
pre-compiled file).
209+
177210
=head1 Slurpy
178211
179212
A parameter of a sub or method is said to be I<slurpy> if it can consume an
180213
arbitrary number of arguments. It is indicated by an asterisk C<*> in front
181214
of the parameter name.
182215
183-
sub sum (*@numbers) {
216+
sub sum(*@numbers) {
184217
return [+] @numbers;
185218
}
186219
220+
This can also be used to collect all possible named parameters in a call:
221+
222+
sub allnameds(*%named) { .say for %named.sort }
223+
allnameds a => 42, :666b, :c<foo>; # a => 42, b => 666, c => foo
224+
187225
=head1 Type Object
188226
189227
A I<type object> is an object representing a class, role, package, grammar

0 commit comments

Comments
 (0)