Skip to content

Commit eb5de2b

Browse files
committed
Adds example and reflow #2703
1 parent 149d850 commit eb5de2b

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

doc/Type/Junction.pod6

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ junctions, you use the string that represents the type followed by any object,
3333
or else call L<C<.all>|/routine/all>, L<C<.none>|/routine/none> or
3434
L<C<.one>|/routine/one> on the object.
3535
36+
say so 3 == (1..30).one;# OUTPUT: «True␤»
37+
3638
Autothreading happens when a junction is bound to a parameter of a code object
3739
that doesn't accept values of type C<Junction>. Instead of producing an error,
3840
the signature binding is repeated for each value of the junction.
@@ -50,10 +52,10 @@ C<False|True>. The C<if> conditional evaluates the junction in boolean
5052
context, which collapses it to C<True>. So the code prints C<yes\n>.
5153
5254
The type of a C<Junction> does I<not> affect the number of items in the
53-
resultant C<Junction> after autothreading. For example, using a L<one|/routine/one>
54-
C<Junction> during L<Hash|/type/Hash> key lookup, still results in a C<Junction>
55-
with several items. It is only in boolean context would the type of the
56-
C<Junction> come into play:
55+
resultant C<Junction> after autothreading. For example, using a
56+
L<one|/routine/one> C<Junction> during L<Hash|/type/Hash> key lookup, still
57+
results in a C<Junction> with several items. It is only in boolean context would
58+
the type of the C<Junction> come into play:
5759
5860
my %h = :42foo, :70bar;
5961
say %h{one <foo meow>}:exists; # OUTPUT: «one(True, False)␤»
@@ -143,32 +145,33 @@ are concerned:
143145
my $j = +any "not a number", "42", "2.1";
144146
(gather $j».take).grep(Numeric).say; # OUTPUT: «(42 2.1)␤»
145147
146-
Above, we've used prefix C<+> operator on a L<Junction|/type/Junction> to coerce the strings
147-
inside of it to L<Numeric|/type/Numeric>. Since the operator returns a L<Failure|/type/Failure> when
148-
L<Str|/type/Str> that doesn't contain a number gets coerced to C<Numeric>, one of the
149-
elements in the C<Junction> is a C<Failure>, but same C<Failure> rules as normal
150-
apply and the C<Failure> doesn't explode just because it's in a C<Junction>, and
151-
we can L«C<.grep>|/routine/grep» it out. The exception I<will> be thrown,
152-
if you try to use the C<Failure> as a value—just like were this C<Failure> on
153-
its own and not part of the C<Junction>:
148+
Above, we've used prefix C<+> operator on a L<Junction|/type/Junction> to coerce
149+
the strings inside of it to L<Numeric|/type/Numeric>. Since the operator returns
150+
a L<Failure|/type/Failure> when L<Str|/type/Str> that doesn't contain a number
151+
gets coerced to C<Numeric>, one of the elements in the C<Junction> is a
152+
C<Failure>, but same C<Failure> rules as normal apply and the C<Failure> doesn't
153+
explode just because it's in a C<Junction>, and we can L«C<.grep>|/routine/grep»
154+
it out. The exception I<will> be thrown, if you try to use the C<Failure> as a
155+
value—just like were this C<Failure> on its own and not part of the C<Junction>:
154156
155157
my $j = +any "not a number", "42", "2.1";
156158
try say $j == 42;
157159
$! and say "Got exception: $!.^name()"; # OUTPUT: «Got exception: X::Str::Numeric␤»
158160
159161
Note that if an exception gets thrown when I<any> of the values in a
160-
L<Junction|/type/Junction> get computed, it will be thrown just as if the problematic value
161-
were computed on its own and not with a C<Junction>; you can't just compute the
162-
values that work while ignoring exceptions:
162+
L<Junction|/type/Junction> get computed, it will be thrown just as if the
163+
problematic value were computed on its own and not with a C<Junction>; you can't
164+
just compute the values that work while ignoring exceptions:
163165
164166
sub calc ($_) { die when 13 }
165167
my $j = any 1..42;
166168
say try calc $j; # OUTPUT: «Nil␤»
167169
168-
Only one value above causes an exception, but the result of the
169-
L«C<try> block|/language/exceptions#index-entry-try_blocks-try» is still a L<Nil|/type/Nil>.
170-
A possible way around it is to cheat and evaluate the values of the C<Junction>
171-
individually and then re-create the C<Junction> from the result:
170+
Only one value above causes an exception, but the result of the L«C<try>
171+
block|/language/exceptions#index-entry-try_blocks-try» is still a
172+
L<Nil|/type/Nil>. A possible way around it is to cheat and evaluate the values
173+
of the C<Junction> individually and then re-create the C<Junction> from the
174+
result:
172175
173176
sub calc ($_) { die when 13 }
174177
my $j = any 1..42;
@@ -198,9 +201,10 @@ Defined as:
198201
199202
multi method Str(Junction:D:)
200203
201-
Autothreads the C<.Str> method over its elements and returns results as a L<Junction|/type/Junction>. Output
202-
methods that use C<.Str> method (L<print|/routine/print> and L<put|/routine/put>) are special-cased to autothread junctions,
203-
despite being able to accept a L<Mu|/type/Mu> type.
204+
Autothreads the C<.Str> method over its elements and returns results as a
205+
L<Junction|/type/Junction>. Output methods that use C<.Str> method
206+
(L<print|/routine/print> and L<put|/routine/put>) are special-cased to
207+
autothread junctions, despite being able to accept a L<Mu|/type/Mu> type.
204208
205209
=head2 method gist
206210

0 commit comments

Comments
 (0)