Skip to content

Commit ecdb5ad

Browse files
committed
Make examples compile, improve signature catching part of extract-examples script.
1 parent c615f9a commit ecdb5ad

13 files changed

+140
-132
lines changed

doc/Type/Exception.pod6

Lines changed: 38 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -28,51 +28,52 @@ Defined as:
2828
2929
method message(Exception:D:) returns Str:D
3030
31-
Usage:
32-
33-
EXCEPTION.message
34-
3531
This is a stub that must be overwritten by subclasses, and should
3632
return the exception message.
3733
3834
Special care should be taken that this method does not produce
3935
an exception itself.
4036
37+
try die "Something bad happend";
38+
if ($!) {
39+
say $!.message; # Something bad happened.
40+
}
41+
4142
=head2 method backtrace
4243
4344
Defined as:
4445
4546
method backtrace(Exception:D:) returns Backtrace:D
4647
47-
Usage:
48-
49-
EXCEPTION.backtrace
50-
5148
Returns the backtrace associated with the exception. Only makes sense
5249
on exceptions that have been thrown at least once.
5350
51+
try die "Something bad happend";
52+
if ($!) {
53+
say $!.backtrace;
54+
}
55+
5456
=head2 method throw
5557
5658
Defined as:
5759
5860
method throw(Exception:D:)
5961
60-
Usage:
61-
62-
EXCEPTION.throw
63-
6462
Throws the exception.
6563
66-
=head2 method resume
64+
my $exception = X::AdHoc.new; # Totally fine
65+
try $exception.throw; # Throws
66+
if ($!) { #`( some handling ) }; # Suppress the exception in a
6767
68-
Resumes control flow where C<.throw> left it when handled in a C<CATCH> block.
68+
=head2 method resume
6969
7070
Defined as:
7171
7272
method resume(Exception:D:)
7373
74-
Usage:
74+
Resumes control flow where C<.throw> left it when handled in a C<CATCH> block.
7575
76+
# For example, resume control flow for any exception
7677
CATCH { default { .resume } }
7778
7879
=head2 method rethrow
@@ -81,27 +82,23 @@ Defined as:
8182
8283
method rethrow(Exception:D:)
8384
84-
Usage:
85-
86-
EXCEPTION.rethrow
87-
8885
Rethrows an exception that has already been thrown at least once.
8986
This is different from C<throw> in that it preserves the original
9087
backtrace.
9188
89+
my $e = X::AdHoc.new(payload => "Bad situation");
90+
sub f() { die 'Bad' };
91+
sub g() { try f; CATCH { default { .rethrow } } };
92+
g;
93+
CATCH { default { say .backtrace.full } };
94+
9295
=head2 method fail
9396
9497
Defined as:
9598
96-
sub fail(*@text)
97-
sub fail(Exception $e)
98-
method fail(Exception:D $e:)
99-
100-
Usage:
101-
102-
fail(TEXT)
103-
fail(EXCEPTION)
104-
EXCEPTION.fail
99+
multi sub fail(*@text)
100+
multi sub fail(Exception $e)
101+
multi method fail(Exception:D $e:)
105102
106103
Exits the calling C<Routine> and returns a L<Failure> object wrapping the
107104
exception C<$e> - or, for the C<*@text> form, an L<X::AdHoc> exception
@@ -120,41 +117,36 @@ Defined as:
120117
121118
multi method gist(Exception:D:)
122119
123-
Usage:
124-
125-
EXCEPTION.gist
126-
127120
Returns whatever the exception printer should produce for this exception.
128121
The default implementation returns message and backtrace separated by
129122
a newline.
130123
124+
my $e = X::AdHoc.new(payload => "This exception is pretty bad");
125+
try $e.throw;
126+
if ($!) { say $!.gist; };
127+
# OUTPUT: «This exception is pretty bad
128+
# in block <unit> at <unknown file> line 1␤»
129+
131130
=head2 sub die
132131
133132
Defined as:
134133
135134
multi sub die(*@message)
136-
multi sub die(Exception:D $e:)
137-
method die(Exception:D $e:)
138-
139-
Usage:
140-
141-
die(MESSAGE)
142-
die(EXCEPTION)
143-
EXCEPTION.die
135+
multi sub die(Exception:D $e)
136+
method die(Exception:D $e)
144137
145138
Throws a fatal C<Exception>. The default exception handler prints each
146139
element of the list to C<$*ERR> (STDERR).
147140
141+
=for code :skip-test
142+
die "Important reason";
143+
148144
=head2 sub warn
149145
150146
Defined as:
151147
152148
multi sub warn(*@message)
153149
154-
Usage:
155-
156-
warn(MESSAGE)
157-
158150
Throws a resumable warning exception, which is considered a control
159151
exception, and hence is invisible to most normal exception handlers. The
160152
outermost control handler will print the warning to C<$*ERR>. After printing
@@ -167,6 +159,8 @@ To simply print to C<$*ERR>, please use C<note> instead. C<warn> should be
167159
reserved for use in threatening situations when you don't quite want to
168160
throw an exception.
169161
162+
warn "Warning message";
163+
170164
=end pod
171165

172166
# vim: expandtab shiftwidth=4 ft=perl6

doc/Type/Grammar.pod6

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@ More L<documentation on grammars|/language/grammars> is available.
2929
3030
Defined as:
3131
32-
method parse($target, :$rule = 'TOP', Capture() :$args = \(), Mu :$actions = Mu, *%opt) {
33-
34-
Usage:
35-
36-
GRAMMAR.parse(TARGET, RULE?, ARGS?, ACTIONS?, OPTIONS?)
32+
method parse($target, :$rule = 'TOP', Capture() :$args = \(), Mu :$actions = Mu, *%opt)
3733
3834
Parses the C<$target> (which will be coerced to L<Str|/type/Str> if it isn't
3935
one), using C<$rule> as the starting rule. Additional C<$args> will be given
@@ -84,35 +80,32 @@ failure.
8480
8581
This outputs:
8682
87-
「Year,Make,Model,Length
88-
1997,Ford,E350,2.34
89-
2000,Mercury,Cougar,2.38
90-
91-
line => 「Year,Make,Model,Length」
92-
value => 「Year」
93-
value => 「Make」
94-
value => 「Model」
95-
value => 「Length」
96-
line => 「1997,Ford,E350,2.34」
97-
value => 「1997」
98-
value => 「Ford」
99-
value => 「E350」
100-
value => 「2.34」
101-
line => 「2000,Mercury,Cougar,2.38 」
102-
value => 「2000」
103-
value => 「Mercury」
104-
value => 「Cougar」
105-
value => 「2.38 」
83+
=for code :skip-test
84+
「Year,Make,Model,Length
85+
1997,Ford,E350,2.34
86+
2000,Mercury,Cougar,2.38
87+
88+
line => 「Year,Make,Model,Length」
89+
value => 「Year」
90+
value => 「Make」
91+
value => 「Model」
92+
value => 「Length」
93+
line => 「1997,Ford,E350,2.34」
94+
value => 「1997」
95+
value => 「Ford」
96+
value => 「E350」
97+
value => 「2.34」
98+
line => 「2000,Mercury,Cougar,2.38 」
99+
value => 「2000」
100+
value => 「Mercury」
101+
value => 「Cougar」
102+
value => 「2.38 」
106103
107104
=head2 method subparse
108105
109106
Defined as:
110107
111-
method subparse($target, :$rule = 'TOP', Capture() :$args = \(), Mu :$actions = Mu, *%opt) {
112-
113-
Usage:
114-
115-
GRAMMAR.subparse(TARGET, RULE?, ARGS?, ACTIONS?, OPTIONS?)
108+
method subparse($target, :$rule = 'TOP', Capture() :$args = \(), Mu :$actions = Mu, *%opt)
116109
117110
Does exactly the same as L<method parse|/routine/parse>, except that cursor
118111
doesn't have to reach the end of the string to succeed. (That is, it doesn't
@@ -129,13 +122,15 @@ have to match the whole string).
129122
130123
Defined as:
131124
132-
method parsefile(Str(Cool) $filename, :$enc, *%opts) {
133-
134-
Usage:
135-
136-
GRAMMAR.parsefile(FILE, OPTIONS?)
125+
method parsefile(Str(Cool) $filename, :$enc, *%opts)
137126
138127
Reads file C<$filename>, and parses it. All named arguments are passed on to
139128
L<method parse|/routine/parse>.
140129
130+
=for code :skip-test
131+
grammar A {
132+
token TOP { a+ };
133+
}
134+
A.parsefile('my-good-file', 'UTF-8');
135+
141136
=end pod

doc/Type/IntStr.pod6

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
=SUBTITLE Dual Value Integer and String
66
7-
class IntStr is Int is Str
7+
class IntStr is Int is Str { }
88
99
The dual value types (often referred to as L<allomorphs|/language/glossary#Allomorph>)
1010
allow for the representation of a value as both a string and a numeric type, typically

doc/Type/Junction.pod6

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ use a L<Set> or a related type instead.
5252
5353
Usage examples:
5454
55+
my @list = <1 2 "Great">;
56+
@list.append(True).append(False);
5557
my @bool_or_int = grep Bool|Int, @list;
5658
5759
sub is_prime(Int $x) returns Bool {
@@ -68,10 +70,11 @@ Special care should be taken when using C<all> with arguments that may
6870
produce an empty list:
6971
7072
my @a = ();
71-
so all(@a) # True, because there are 0 False's
73+
say so all(@a) # True, because there are 0 False's
7274
7375
To express "all, but at least one", you can use C<@a && all(@a)>
7476
77+
my @a = ();
7578
say so @a && all(@a); # False
7679
7780
Negated operators are special-cased when it comes to autothreading.

doc/Type/Rat.pod6

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
=SUBTITLE Rational number (limited-precision)
66
7-
class Rat is Cool does Rational[Int, UInt64] { ... }
7+
class Rat is Cool does Rational[Int, UInt64] { }
88
99
C<Rat> objects store rational numbers as a pair of a numerator and
1010
denominator. Number literals with a dot but without exponent produce

doc/Type/Rational.pod6

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,22 @@ Built into Perl 6 are L<Rat> and L<FatRat>, which both do the C<Rational> role.
1616
1717
=head2 method new
1818
19-
method new(NuT:D $numerator, DeT:D $denominator) returns Rational:D
19+
=for code :skip-test
20+
method new(NuT:D: $numerator, DeT:D: $denominator) returns Rational:D
2021
2122
Creates a new rational object from numerator and denominator.
2223
2324
=head2 method numerator
2425
25-
method numerator(Rational:D:) returns NuT:D
26+
=for code :skip-test
27+
method numerator(Rational:D:) returns NuT:D
2628
2729
Returns the numerator.
2830
2931
=head2 method denominator
3032
31-
method denominator(Rational:D:) returns DeT:D
33+
=for code :skip-test
34+
method denominator(Rational:D:) returns DeT:D
3235
3336
Returns the denominator.
3437

doc/Type/Real.pod6

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Rounds the number towards zero.
6464
Converts the number to a string, using C<$base> as base. For C<$base> larger
6565
than ten, capital Latin letters are used.
6666
67-
255.base(16) # 'FF'
67+
255.base(16); # 'FF'
6868
6969
The optional C<$digits> argument asks for that many digits of fraction
7070
(which may not be negative). If omitted, a reasonable default is chosen
@@ -79,7 +79,7 @@ The final digit produced is always rounded.
7979
To convert a string to a number use the C<:16($string)> notation where 16 is
8080
the base:
8181
82-
say :16("FF") # 255
83-
say :23("FF") # 360
82+
say :16("FF"); # 255
83+
say :23("FF"); # 360
8484
8585
=end pod

doc/Type/Semaphore.pod6

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,18 @@ printer becomes available.
1717
1818
method BUILD( Int:D :$nbr-printers ) {
1919
for ^$nbr-printers -> $pc {
20-
$!printers[$pc] = { :name{"printer-$pc"}, ... };
20+
$!printers[$pc] = { :name{"printer-$pc"} };
2121
}
2222
2323
$!print-control .= new($nbr-printers);
2424
}
2525
26+
method find-available-printer-and-print-it($job) { say "Is printed!"; }
27+
2628
method print( $print-job ) {
2729
$!print-control.acquire;
2830
29-
find-available-printer-and-print-it($job);
31+
self.find-available-printer-and-print-it($print-job);
3032
3133
$!print-control.release;
3234
}

doc/Type/Set.pod6

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ C<Set>s can be treated as object hashes using the C<{ }> postcircumfix operator,
2424
which returns the value C<True> for keys that are elements of the set, and
2525
C<False> for keys that aren't:
2626
27+
my $fruits = set <peach apple orange apple apple>;
2728
say $fruits<apple>; # True
2829
say $fruits<kiwi>; # False
2930

doc/Type/SetHash.pod6

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ and C<False> for keys that aren't. Assigning a value that boolifies to C<True>
2626
or C<False>, respectively, can be used to add or remove a set element:
2727
2828
=begin code
29+
my $fruits = <peach apple orange apple apple>.SetHash;
30+
2931
say $fruits<apple>; # True
3032
say $fruits<kiwi>; # False
3133

0 commit comments

Comments
 (0)