Skip to content

Commit 67b1dfa

Browse files
authored
Merge pull request #1181 from perl6/language-section-examples2
Unify Language/ examples output style
2 parents 566bdd9 + e842f77 commit 67b1dfa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1060
-1017
lines changed

doc/Language/classtut.pod6

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ A default constructor allows the setting of attributes for the created object:
3030
# Create a new Rectangle from two Points
3131
my $r = Rectangle.new(lower => Point.new(x => 0, y => 0), upper => Point.new(x => 10, y => 10));
3232
33-
say $r.area(); # -> 100
33+
say $r.area(); # OUTPUT: «100␤»
3434
=end code
3535
3636
You can also provide your own construction and build
@@ -114,19 +114,19 @@ method.
114114
You can use C<.DEFINITE> method to find out if what you have is an instance
115115
or a type object:
116116
117-
say Int.DEFINITE; # False (type object)
118-
say 426.DEFINITE; # True (instance)
117+
say Int.DEFINITE; # OUTPUT: «False␤» (type object)
118+
say 426.DEFINITE; # OUTPUT: «True␤» (instance)
119119
120120
class Foo {};
121-
say Foo.DEFINITE; # False (type object)
122-
say Foo.new.DEFINITE; # True (instance)
121+
say Foo.DEFINITE; # OUTPUT: «False␤» (type object)
122+
say Foo.new.DEFINITE; # OUTPUT: «True␤» (instance)
123123
124124
You can also use type smileys to only accept instances or type objects:
125125
126126
multi foo (Int:U) { "It's a type object!" }
127127
multi foo (Int:D) { "It's an instance!" }
128-
say foo Int; # It's a type object!
129-
say foo 42; # It's an instance!
128+
say foo Int; # OUTPUT: «It's a type object!␤»
129+
say foo 42; # OUTPUT: «It's an instance!␤»
130130
131131
=head1 State
132132
@@ -537,14 +537,14 @@ class overriding the C<Cook>'s C<cook> method.
537537
cookbooks => 'The Joy of Cooking',
538538
salary => 40000);
539539
540-
$cook.cook( 'pizza' ); # Cooking pizza
540+
$cook.cook( 'pizza' ); # OUTPUT: «Cooking pizza␤»
541541
542542
my $baker = Baker.new(
543543
utensils => 'self cleaning oven',
544544
cookbooks => "The Baker's Apprentice",
545545
salary => 50000);
546546
547-
$baker.cook('brioche'); # Baking a tasty brioche
547+
$baker.cook('brioche'); # OUTPUT: «Baking a tasty brioche␤»
548548
=end code
549549
550550
Because the dispatcher will see the C<cook> method on C<Baker> before it

doc/Language/concurrency.pod6

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,18 @@ in a concurrent or asynchronous manner.
4545
4646
=begin code
4747
my $p1 = Promise.new;
48-
say $p1.status; # Planned;
48+
say $p1.status; # OUTPUT: «Planned␤»
4949
$p1.keep('result');
50-
say $p1.status; # Kept
51-
say $p1.result; # result
50+
say $p1.status; # OUTPUT: «Kept␤»
51+
say $p1.result; # OUTPUT: «result␤»
5252
# (since it has been kept, a result is available!)
5353
5454
my $p2 = Promise.new;
5555
$p2.break('oh no');
56-
say $p2.status; # Broken
57-
say $p2.result; # dies with "oh no", because the promise has been broken
56+
say $p2.status; # OUTPUT: «Broken␤»
57+
say $p2.result; # dies, because the promise has been broken
58+
CATCH { default { say .^name, ': ', .Str } };
59+
# OUTPUT: «X::AdHoc+{X::Promise::Broken}: oh no␤»
5860
=end code
5961
6062
Promises gain much of their power by being composable, for example by
@@ -65,7 +67,7 @@ chaining, usually by the L<then|/type/Promise#method_then> method:
6567
-> $v { say $v.result; "Second Result" }
6668
);
6769
$promise1.keep("First Result");
68-
say $promise2.result; # First Result \n Second Result
70+
say $promise2.result; # OUTPUT: «First ResultSecond Result␤»
6971
7072
Here the L<then|/type/Promise#method_then> method schedules code to be
7173
executed when the first L<Promise> is kept or broken, itself returning
@@ -82,7 +84,7 @@ is illustrated with:
8284
my $promise2 = $promise1.then(-> $v { say "Handled but : "; say $v.result});
8385
$promise1.break("First Result");
8486
try $promise2.result;
85-
say $promise2.cause; # Handled but : \n First Result
87+
say $promise2.cause; # OUTPUT: «Handled but : First Result␤»
8688
8789
Here the C<break> will cause the code block of the C<then> to throw an
8890
exception when it calls the C<result> method on the original promise
@@ -110,7 +112,7 @@ for that:
110112
my $promise = Promise.start(
111113
{ my $i = 0; for 1 .. 10 { $i += $_ }; $i}
112114
);
113-
say $promise.result; # 55
115+
say $promise.result; # OUTPUT: «55␤»
114116
115117
Here the C<result> of the promise returned is the value returned from
116118
the code. Similarly if the code fails (and the promise is thus broken),
@@ -152,7 +154,7 @@ the result of each:
152154
$i
153155
};
154156
my @result = await $p1, $p2;
155-
say @result; # 55 -55
157+
say @result; # OUTPUT: «[55 -55]␤»
156158
157159
In addition to C<await>, two class methods combine several
158160
L<Promise> objects into a new promise: C<allof> returns a promise that
@@ -217,6 +219,8 @@ as the vow object is kept private, the status of the promise is safe:
217219
# Will throw an exception
218220
# "Access denied to keep/break this Promise; already vowed"
219221
$promise.keep;
222+
CATCH { default { say .^name, ': ', .Str } };
223+
# OUTPUT: «X::Promise::Vowed: Access denied to keep/break this Promise; already vowed␤»
220224
221225
The methods that return a promise that will be kept or broken
222226
automatically such as C<in> or C<start> will do this, so it is not
@@ -421,7 +425,7 @@ and returns it, blocking until a new item is sent if the queue is empty:
421425
422426
my $channel = Channel.new;
423427
$channel.send('Channel One');
424-
say $channel.receive; # 'Channel One'
428+
say $channel.receive; # OUTPUT: «Channel One␤»
425429
426430
If the channel has been closed with the L<method
427431
close|/type/Channel#method_close> then any C<send> will cause the
@@ -758,7 +762,7 @@ at a time:
758762
}
759763
}
760764
761-
say $a; # 10
765+
say $a; # OUTPUT: «10␤»
762766
763767
C<protect> returns whatever the code block returns.
764768

doc/Language/containers.pod6

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Note that subroutine signatures allow passing around of containers:
5252
}
5353
my $x = 42;
5454
f($x);
55-
say $x; # 23
55+
say $x; # OUTPUT: «23␤»
5656
5757
Inside the subroutine, the lexpad entry for C<$a> points to the same
5858
container that C<$x> points to outside the subroutine. Which is why
@@ -63,7 +63,7 @@ Likewise a routine can return a container if it is marked as C<is rw>:
6363
my $x = 23;
6464
sub f() is rw { $x };
6565
f() = 42;
66-
say $x; # 42
66+
say $x; # OUTPUT: «42␤»
6767
6868
For explicit returns, C<return-rw> instead of C<return> must be used.
6969
@@ -84,16 +84,16 @@ Scalar containers are transparent to type checks and most kinds of read-only
8484
accesses. A C<.VAR> makes them visible:
8585
8686
my $x = 42;
87-
say $x.^name; # Int
88-
say $x.VAR.^name; # Scalar
87+
say $x.^name; # OUTPUT: «Int␤»
88+
say $x.VAR.^name; # OUTPUT: «Scalar␤»
8989
9090
And C<is rw> on a parameter requires the presence of a writable Scalar
9191
container:
9292
9393
sub f($x is rw) { say $x };
9494
f 42;
9595
CATCH { default { say .^name, ': ', .Str } };
96-
# OUTPUT«X::Parameter::RW: Parameter '$x' expected a writable container, but got Int value␤»
96+
# OUTPUT: «X::Parameter::RW: Parameter '$x' expected a writable container, but got Int value␤»
9797
9898
=head1 Binding
9999
@@ -109,15 +109,15 @@ means that you cannot assign to it anymore:
109109
my $x := 42;
110110
$x = 23;
111111
CATCH { default { say .^name, ': ', .Str } };
112-
# OUTPUT«X::AdHoc: Cannot assign to an immutable value␤»
112+
# OUTPUT: «X::AdHoc: Cannot assign to an immutable value␤»
113113
114114
You can also bind variables to other variables:
115115
116116
my $a = 0;
117117
my $b = 0;
118118
$a := $b;
119119
$b = 42;
120-
say $a; # 42
120+
say $a; # OUTPUT: «42␤»
121121
122122
Here, after the initial binding, the lexpad entries for C<$a> and C<$b> both
123123
point to the same scalar container, so assigning to one variable also
@@ -132,32 +132,30 @@ Sigilless variables also bind by default and so do parameters with the trait C<i
132132
my $a = 42;
133133
my \b = $a;
134134
b++;
135-
say $a; # 43
135+
say $a; # OUTPUT: «43␤»
136136
137137
sub f($c is raw) { $c++ }
138138
f($a);
139-
say $a; # 44
139+
say $a; # OUTPUT: «44␤»
140140
141141
=head1 Scalar containers and listy things
142142
143143
There are a number of positional container types with slightly different
144144
semantics in Perl 6. The most basic one is L<List|/type/List>
145145
It is created by the comma operator.
146146
147-
say (1, 2, 3).^name; # List
147+
say (1, 2, 3).^name; # OUTPUT: «List␤»
148148
149149
A list is immutable, which means you cannot change the number of elements
150150
in a list. But if one of the elements happens to be a scalar container,
151151
you can still assign to it:
152152
153153
my $x = 42;
154154
($x, 1, 2)[0] = 23;
155-
say $x; # 23
156-
($x, 1, 2)[1] = 23; # Error: Cannot modify an immutable value
155+
say $x; # OUTPUT: «23␤»
156+
($x, 1, 2)[1] = 23; # Cannot modify an immutable value
157157
CATCH { default { say .^name, ': ', .Str } };
158-
# OUTPUT:
159-
# 23
160-
# X::Assignment::RO: Cannot modify an immutable Int
158+
# OUTPUT: «X::Assignment::RO: Cannot modify an immutable Int␤»
161159
162160
So the list doesn't care about whether its elements are values or
163161
containers, they just store and retrieve whatever was given to them.
@@ -170,7 +168,7 @@ be containers, which means that you can always assign to elements:
170168
171169
my @a = 1, 2, 3;
172170
@a[0] = 42;
173-
say @a; # 42 2 3
171+
say @a; # OUTPUT: «[42 2 3]␤»
174172
175173
C<@a> actually stores three scalar containers. C<@a[0]> returns one of
176174
them, and the assignment operator replaces the integer value stored in that
@@ -186,8 +184,8 @@ the same thing: discard the old value(s), and enter some new value(s).
186184
187185
Nevertheless, it's easy to observe how different they are:
188186
189-
my $x = 42; say $x.^name; # Int
190-
my @a = 42; say @a.^name; # Array
187+
my $x = 42; say $x.^name; # OUTPUT: «Int␤»
188+
my @a = 42; say @a.^name; # OUTPUT: «Array␤»
191189
192190
This is because the C<Scalar> container type hides itself well, but C<Array>
193191
makes no such effort. Also assignment to an array variable is coercive, so
@@ -196,7 +194,7 @@ you can assign a non-array value to an array variable.
196194
To place a non-C<Array> into an array variable, binding works:
197195
198196
my @a := (1, 2, 3);
199-
say @a.WHAT; # (List)
197+
say @a.WHAT; # OUTPUT: «(List)␤»
200198
201199
=head1 Binding to array elements
202200
@@ -205,7 +203,7 @@ As a curious side note, Perl 6 supports binding to array elements:
205203
my @a = (1, 2, 3);
206204
@a[0] := my $x;
207205
$x = 42;
208-
say @a; # 42 2 3
206+
say @a; # OUTPUT: «[42 2 3]␤»
209207
210208
If you've read and understood the previous explanations, it is now time to
211209
wonder how this can possibly work. After all, binding to a variable requires
@@ -229,7 +227,7 @@ counter-intuitive results when the array is used later.
229227
@a[2] = $b; # ...but this is fine.
230228
@a[1, 2] := 1, 2; # runtime error: X::Bind::Slice
231229
CATCH { default { say .^name, ': ', .Str } };
232-
# OUTPUT«X::Bind::Slice: Cannot bind to Array slice␤»
230+
# OUTPUT: «X::Bind::Slice: Cannot bind to Array slice␤»
233231
234232
Operations that mix Lists and Arrays generally protect against such
235233
a thing happening accidentally.
@@ -249,23 +247,23 @@ do not flatten in list context:
249247
250248
my @a = 1, 2, 3;
251249
my @b = @a, 4, 5;
252-
say @b.elems; # 3
250+
say @b.elems; # OUTPUT: «3␤»
253251
254252
There are operations that flatten out sublists that are not inside a scalar
255253
container: slurpy parameters (C<*@a>) and explicit calls to C<flat>:
256254
257255
258256
my @a = 1, 2, 3;
259-
say (flat @a, 4, 5).elems; # 5
257+
say (flat @a, 4, 5).elems; # OUTPUT: «5␤»
260258
261259
sub f(*@x) { @x.elems };
262-
say f @a, 4, 5; # 5
260+
say f @a, 4, 5; # OUTPUT: «5␤»
263261
264262
As hinted above, scalar containers prevent that flattening:
265263
266264
sub f(*@x) { @x.elems };
267265
my @a = 1, 2, 3;
268-
say f $@a, 4, 5; # 3
266+
say f $@a, 4, 5; # OUTPUT: «3␤»
269267
270268
The C<@> character can also be used as a prefix to remove a scalar container:
271269
@@ -289,15 +287,15 @@ user to L<handle|/type/Promise#method_in> timeouts.
289287
my @a;
290288
@a[0] = @a;
291289
put @a.perl;
292-
# OUTPUT«(my \Array_54878224 = [Array_54878224,])»
290+
# OUTPUT: «(my \Array_54878224 = [Array_54878224,])»
293291
294292
=head1 Type Constraints
295293
296294
Any container can have a type constraint in form of a L<type object|/language/typesystem#Type_objects> or a L<subset|/language/typesystem#subset>. Both can be place between a declarator and the variable name or after the trait L<of|/type/Variable#trait_is_dynamic>. The constraint is a property of the container, not the variable. Any (re-)binding may change the type constraint or remove the constraint altogether if bound to a value instead of a container. Introspection of type constraints on containers is provided by C<.VAR.of>.
297295
298296
EVAL ‚my $i = 42; say $i.VAR.of; $i := "forty plus two"; say $i.VAR.of;‘;
299297
CATCH { default { say .^name, ' ', .Str } }
300-
# OUTPUT«(Mu)␤X::Method::NotFound No such method 'of' for invocant of type 'Str'␤»
298+
# OUTPUT: «(Mu)␤X::Method::NotFound No such method 'of' for invocant of type 'Str'␤»
301299
302300
303301
=head1 Custom containers
@@ -322,9 +320,9 @@ work with types in Perl 6.
322320
}
323321
324322
my Int $a := lucky(Int);
325-
say $a = 12;
323+
say $a = 12; # OUTPUT: «12␤»
326324
say $a = 'FOO'; # X::TypeCheck::Binding
327-
say $a = 13; # X::OutOfRange
328-
CATCH { default { say .^name, ': ', .Str; .resume } };
325+
say $a = 13; # X::OutOfRange
326+
CATCH { default { say .^name, ': ', .Str } };
329327
330328
=end pod

0 commit comments

Comments
 (0)