Skip to content

Commit e83de06

Browse files
committed
Make examples compile
1 parent a57a821 commit e83de06

File tree

9 files changed

+137
-100
lines changed

9 files changed

+137
-100
lines changed

doc/Type/Callable.pod6

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ of such a container is C<Callable>. A signature object can be used to
1414
force a check against the signature of the Callable to be stored into the
1515
container.
1616
17-
my &a = {};
17+
my &a = {;}; # Empty block needs a semicolon
1818
my &b = -> {};
1919
my &c = sub () {};
2020
sub foo() {};

doc/Type/Failure.pod6

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,47 +30,43 @@ Defined as:
3030
3131
method handled(Failure:D:) returns Bool:D
3232
33-
Usage:
34-
35-
FAILURE.handled
36-
3733
Returns C<True> for handled failures, C<False> otherwise.
3834
35+
sub f() { fail }; my $v = f; say $v.handled; # False
36+
3937
=head2 method exception
4038
4139
Defined as:
4240
4341
method exception(Failure:D:) returns Exception
4442
45-
Usage:
46-
47-
FAILURE.exception
48-
4943
Returns the L<Exception> object that the failure wraps.
5044
45+
sub f() { fail }; my $v = f; $v.Exception;
46+
CATCH { default { put .^name, ': ', .Str } };
47+
# OUTPUT: «X::AdHoc: Failed␤»
48+
5149
=head2 method Bool
5250
5351
Defined as:
5452
5553
multi method Bool(Failure:D:) returns Bool:D
5654
57-
Usage:
58-
59-
FAILURE.Bool
60-
6155
Returns C<False>, and marks the failure as handled.
6256
57+
sub f() { fail }; my $v = f; say $v.handled; $v.Bool; say $v.handled;
58+
# False
59+
# True
60+
6361
=head2 method defined
6462
6563
Defined as:
6664
6765
multi method defined(Failure:D:) returns Bool:D
6866
69-
Usage:
70-
71-
FAILURE.defined
72-
7367
Returns C<False> (failures are officially undefined), and marks
7468
the failure as handled.
7569
70+
sub f() { fail }; my $v = f; say $v.defined; # False
71+
7672
=end pod

doc/Type/Nil.pod6

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,41 +14,41 @@ so smartmatching C<Nil> will also match C<Failure>.)
1414
1515
The class C<Nil> is the same exact thing as its only possible value, C<Nil>.
1616
17-
say Nil === Nil.new #-> True
17+
say Nil === Nil.new # True
1818
1919
Along with C<Failure>, C<Nil> and its sub classes may always be returned from a
2020
routine even when the routine specifies a particular return type. It may also
2121
be returned regardless of the definedness of the return type, however, C<Nil>
2222
is considered undefined for all other purposes.
2323
2424
sub a( --> Int:D ) { return Nil }
25-
a().say; #-> Nil
25+
a().say; # Nil
2626
2727
C<Nil> is what is returned from empty routines or clauses, or routines
2828
that use a bare C<return> statement.
2929
30-
sub a { }; a().say; #-> Nil
31-
sub b { return }; b().say; #-> Nil
32-
say (if 1 { }); #-> Nil
33-
{ ; }().say; #-> Nil
34-
say EVAL ""; #-> Nil
30+
sub a { }; a().say; # Nil
31+
sub b { return }; b().say; # Nil
32+
say (if 1 { }); # Nil
33+
{ ; }().say; # Nil
34+
say EVAL ""; # Nil
3535
3636
In a list, C<Nil> takes the space of one value. Iterating a C<Nil>
3737
behaves like iteration of any non-iterable value, producing a sequence
3838
of one C<Nil>. (When you need the other meaning, the special value
3939
C<Empty|/type/Slip#Empty> is available to take no spaces when inserted into
4040
list, and to return no values when iterated.)
4141
42-
(1, Nil, 3).elems.say; #-> 3
43-
(for Nil { $_ }).perl.say; #-> (Nil,)
42+
(1, Nil, 3).elems.say; # 3
43+
(for Nil { $_ }).perl.say; # (Nil,)
4444
4545
Any method call on C<Nil> other than those specifically listed below,
4646
and consequently, any subscripting operation, will succeed and return
4747
C<Nil>.
4848
49-
say Nil.ITotallyJustMadeThisUp; #-> Nil
50-
say (Nil)[100]; #-> Nil
51-
say (Nil){100}; #-> Nil
49+
say Nil.ITotallyJustMadeThisUp; # Nil
50+
say (Nil)[100]; # Nil
51+
say (Nil){100}; # Nil
5252
5353
When assigned to a container, the C<Nil> value (but not any subclass of
5454
C<Nil>) will attempt to revert the container to its default value; if no
@@ -60,37 +60,53 @@ type unless it happens to be a parent class of C<Failure>.)
6060
6161
my Int $x = 42;
6262
$x = Nil;
63-
$x.say; #-> (Int)
63+
$x.say; # (Int)
6464
6565
sub f( --> Int:D ){ Nil }; # this definedness constraint is ignored
6666
my Int:D $i = f; # this definedness contraint is not ignored, so throws
67+
CATCH { default { put .^name, ': ', .Str } };
68+
# OUTPUT: «X::TypeCheck::Assignment: Type check failed in assignment to $y; expected Int but got Any (Any)»
6769
6870
sub g( --> Int:D ){ fail "oops" }; # this definedness constraint is ignored
69-
my Any:D $i = g; # failure object matches Any:D, so is assigned
70-
my Int:D $j = g; # failure object doesn't match Int:D, so throws
71+
my Any:D $h = g; # failure object matches Any:D, so is assigned
72+
73+
but
74+
75+
=for code :skip-test
76+
my Int:D $j = g;
77+
# It will throw both exceptions:
78+
# Earlier failure:
79+
# oops
80+
# in sub g at <unknown file> line 1
81+
# in block <unit> at <unknown file> line 1
82+
#
83+
# Final error:
84+
# Type check failed in assignment to $j; expected Int:D but got Failure (Failure.new(exception...)
85+
# in block <unit> at <unknown file> line 1
7186
7287
Because an untyped variable is type C<Any>, assigning a C<Nil> to one
7388
will result in an L<(Any)|/type/Any> type object.
7489
7590
my $x = Nil;
76-
$x.say; #-> (Any)
77-
my Int $y = $x; # error: Type check failed in assignment to '$y';
78-
# expected 'Int' but got 'Any'
91+
$x.say; # (Any)
92+
my Int $y = $x; # error
93+
CATCH { default { put .^name, ': ', .Str } };
94+
# OUTPUT: «X::TypeCheck::Assignment: Type check failed in assignment to $y; expected Int but got Any (Any)␤»
7995
8096
If you are looking for a variable which transforms objects into type objects
8197
when said variable appears on the RHS, you can type the container as C<Nil>.
8298
8399
my Nil $x;
84100
my Str $s = $x;
85-
$s.say; #-> (Str)
101+
$s.say; # (Str)
86102
87103
There is an important exception to this transforms-into-type-object rule:
88104
assigning C<Nil> to a variable which has a default will restore that default.
89105
90106
my Int $x is default(42) = -1;
91107
my $y = 1;
92108
for $x, $y -> $val is rw { $val = Nil unless $val > 0 }
93-
$x.say; #-> 42
109+
$x.say; # 42
94110
95111
=head1 Methods
96112

doc/Type/Parameter.pod6

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ Returns C<True> for raw parameters.
100100
raw = 5;
101101
}
102102
f(my $x); # works
103-
f(42); # dies in the assignment
103+
f(42); # dies
104+
CATCH { default { put .^name, ': ', .Str } };
105+
# OUTPUT: «X::Assignment::RO: Cannot modify an immutable Int␤»
104106
105107
Raw parameters bind either a variable or a value passed to it, with
106108
no decontainerization taking place. That means that if a variable was passed
@@ -171,19 +173,21 @@ The type used may change from call to call. Once they are defined,
171173
type captures can be used wherever you would use a type, even later
172174
in same the signature:
173175
174-
sub c(::T $x, T $y, $z) { my T $zz = $z };
175-
c(4, 5, 6); # OK
176-
c(4, 5, "six"); # Fails when assigning to $zz, wants Int not Str
177-
c("four", 5, "six"); # Fails when binding $y, wants Str, not Int
176+
=for code :skip-test
177+
sub c(::T $x, T $y, $z) { my T $zz = $z };
178+
c(4, 5, 6); # OK
179+
c(4, 5, "six"); # Fails when assigning to $zz, wants Int not Str
180+
c("four", 5, "six"); # Fails when binding $y, wants Str, not Int
178181
179182
Type captures may be used at the same time as
180183
L<type constraints|/type/Signature#Type_Constraints>.
181184
182-
sub d(::T Numeric $x, T $y);
183-
d(4, 5); # OK
184-
d(4e0, 5e0); # OK
185-
d(4e0, 5); # Fails when binding $y
186-
d("four", "five"); # Fails when binding $x
185+
=for code :skip-test
186+
sub d(::T Numeric $x, T $y);
187+
d(4, 5); # OK
188+
d(4e0, 5e0); # OK
189+
d(4e0, 5); # Fails when binding $y
190+
d("four", "five"); # Fails when binding $x
187191
188192
=head2 method sub_signature
189193

doc/Type/Range.pod6

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
55
=SUBTITLE Interval of ordered values
66
7-
class Range is Iterable does Positional {}
7+
=for code :skip-test
8+
class Range is Iterable does Positional {}
89
910
Ranges serve two main purposes: to generate lists of consecutive
1011
numbers or strings, and to act as a matcher to check if a number
@@ -14,15 +15,16 @@ Ranges are constructed using one of the four possible range operators,
1415
which consist of two dots, and optionally a caret which indicates that
1516
the endpoint marked with it is excluded from the range.
1617
17-
1 .. 5 # 1 <= $x <= 5
18-
1^.. 5 # 1 < $x <= 5
19-
1 ..^5 # 1 <= $x < 5
20-
1^..^5 # 1 < $x < 5
18+
1 .. 5; # 1 <= $x <= 5
19+
1^.. 5; # 1 < $x <= 5
20+
1 ..^5; # 1 <= $x < 5
21+
1^..^5; # 1 < $x < 5
2122
2223
The caret is also a prefix operator for constructing numeric ranges
2324
starting from zero:
2425
25-
^$x # same as 0 ..^ $x.Numeric
26+
my $x = 10;
27+
say ^$x; # same as 0 ..^ $x.Numeric
2628
2729
Iterating a range (or calling the C<list> method) uses the same semantics as
2830
the C<++> prefix and postfix operators, i.e., it calls the C<succ> method on
@@ -31,20 +33,21 @@ the start point, and then the generated elements.
3133
Ranges always go from small to larger elements; if the start point is bigger
3234
than the end point, the range is considered empty.
3335
34-
for 1..5 { .say } # five iterations
35-
('a' ^..^ 'f').list # 'b', 'c', 'd', 'e'
36-
5 ~~ ^5; # False
37-
4.5 ~~ 0..^5 # True
38-
(1.1..5).list; # (1.1, 2.1, 3.1, 4.1)
36+
for 1..5 { .say }; # five iterations
37+
('a' ^..^ 'f').list; # 'b', 'c', 'd', 'e'
38+
5 ~~ ^5; # False
39+
4.5 ~~ 0..^5; # True
40+
(1.1..5).list; # (1.1, 2.1, 3.1, 4.1)
3941
4042
Use the C<...> sequence operator to produce lists of elements that
4143
go from larger to smaller values, or to use offsets other than
4244
increment-by-1.
4345
4446
Use C<Inf> or C<*> (Whatever) to indicate an end point to be open-ended.
4547
46-
for 1..* { .say } # start from 1, continue until stopped
47-
for 1..Inf { .say } # the same
48+
=for code :skip-test
49+
for 1..* { .say }; # start from 1, continue until stopped
50+
for 1..Inf { .say }; # the same
4851
4952
=head2 Ranges in subscripts
5053
@@ -138,7 +141,9 @@ L<excludes-max> are C<True> in which case a L<Failure|/type/Failure> is returned
138141
say $r3.is-int, ', ', $r4.is-int; # False, False
139142
say $r3.excludes-max, ', ', $r4.excludes-max; # False, True
140143
say $r3.minmax; # (1.1 5.2)
141-
say $r4.minmax; # Cannot return minmax on Range with excluded ends
144+
say $r4.minmax;
145+
CATCH { default { put .^name, ': ', .Str } };
146+
# OUTPUT: «X::AdHoc: Cannot return minmax on Range with excluded ends␤»
142147
143148
=head2 method elems
144149
@@ -162,16 +167,16 @@ Generates the list of elements that the range represents.
162167
163168
=head2 method pick
164169
165-
method pick(Range:D: ) returns Any:D
166-
method pick(Range:D: $number) returns Seq:D
170+
multi method pick(Range:D: ) returns Any:D
171+
multi method pick(Range:D: $number) returns Seq:D
167172
168173
Performs the same function as C<Range.list.pick>, but attempts to optimize
169174
by not actually generating the list if it is not necessary.
170175
171176
=head2 method roll
172177
173-
method roll(Range:D: ) returns Any:D
174-
method roll(Range:D: $number) returns Seq:D
178+
multi method roll(Range:D: ) returns Any:D
179+
multi method roll(Range:D: $number) returns Seq:D
175180
176181
Performs the same function as C<Range.list.roll>, but attempts to optimize
177182
by not actually generating the list if it is not necessary.

doc/Type/Scalar.pod6

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,31 @@ tell if this has been done by examining the introspective pseudo-method
3131
C<.VAR>:
3232
3333
my $a = 1;
34-
$a.WHAT.say; # says "(Int)"
35-
$a.VAR.WHAT.say; # says "(Scalar)"
34+
$a.WHAT.say; # (Int)
35+
$a.VAR.WHAT.say; # (Scalar)
3636
my $b := 1;
37-
$b.WHAT.say; # says "(Int)"
38-
$b.VAR.WHAT.say; # says "(Int)"
37+
$b.WHAT.say; # (Int)
38+
$b.VAR.WHAT.say; # (Int)
3939
4040
This same thing happens when values are assigned to an element of
4141
an C<Array>, however, C<List>s directly contain their values:
4242
4343
my @a = 1, 2, 3;
44-
@a[0].WHAT.say; # says "(Int)"
45-
@a[0].VAR.WHAT.say; # says "(Scalar)"
46-
[1, 2, 3][0].WHAT.say; # says "(Int)"
47-
[1, 2, 3][0].VAR.WHAT.say; # says "(Scalar)"
48-
(1, 2, 3)[0].WHAT.say; # says "(Int)"
49-
(1, 2, 3)[0].VAR.WHAT.say; # says "(Int)"
44+
@a[0].WHAT.say; # (Int)
45+
@a[0].VAR.WHAT.say; # (Scalar)
46+
[1, 2, 3][0].WHAT.say; # (Int)
47+
[1, 2, 3][0].VAR.WHAT.say; # (Scalar)
48+
(1, 2, 3)[0].WHAT.say; # (Int)
49+
(1, 2, 3)[0].VAR.WHAT.say; # (Int)
5050
5151
Array elements may be bound directly to values using C<:=> as well,
5252
however this is to be discouraged as it may lead to confusion.
5353
Doing so will break exact round-tripping of C<.perl> output -- since
5454
C<Array>s are assumed to place C<Scalar>s around each element,
5555
C<Scalar>s are not denoted with C<$> in the output of C<Array.perl>.
5656
57-
[1, $(2, 3)].perl.say # says [1, (2, 3)]
58-
(1, $(2, 3)).perl.say # says (1, $(2, 3))
57+
[1, $(2, 3)].perl.say; # [1, (2, 3)]
58+
(1, $(2, 3)).perl.say; # (1, $(2, 3))
5959
6060
Binding a Scalar to a C<$>-sigiled variable replaces the existing
6161
C<Scalar> in that variable, if any, with the given C<Scalar>.
@@ -66,7 +66,7 @@ alter the value of both variables by altering only one of them:
6666
my $a = 1;
6767
my $b := $a;
6868
$b = 2;
69-
$a.say; # says "2"
69+
$a.say; # 2
7070
7171
X<|\ (sigilless scalar)>
7272
SSA-style constants bind directly to their value with no
@@ -76,13 +76,15 @@ to them, at which point, they behave entirely like C<$>-sigiled
7676
variables.
7777
7878
my \c = 1;
79-
c.WHAT.say; # says "(Int)"
80-
c.VAR.WHAT.say; # says "(Int)"
79+
c.WHAT.say; # (Int)
80+
c.VAR.WHAT.say; # (Int)
8181
my $a = 1;
8282
my \d = $a; # just "my \d = $ = 1" works, too
83-
d.WHAT.say; # says "(Int)"
84-
d.VAR.WHAT.say; # says "(Scalar)"
83+
d.WHAT.say; # (Int)
84+
d.VAR.WHAT.say; # (Scalar)
8585
d = 2; # ok
86-
c = 2; # error, cannot modify an immutable Int
86+
c = 2;
87+
CATCH { default { put .^name, ': ', .Str } };
88+
# OUTPUT: «X::Assignment::RO: Cannot modify an immutable Int␤»
8789
8890
=end pod

0 commit comments

Comments
 (0)