@@ -33,6 +33,8 @@ junctions, you use the string that represents the type followed by any object,
33
33
or else call L < C < .all > |/routine/all> , L < C < .none > |/routine/none> or
34
34
L < C < .one > |/routine/one> on the object.
35
35
36
+ say so 3 == (1..30).one;# OUTPUT: «True»
37
+
36
38
Autothreading happens when a junction is bound to a parameter of a code object
37
39
that doesn't accept values of type C < Junction > . Instead of producing an error,
38
40
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
50
52
context, which collapses it to C < True > . So the code prints C < yes\n > .
51
53
52
54
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:
57
59
58
60
my %h = :42foo, :70bar;
59
61
say %h{one <foo meow>}:exists; # OUTPUT: «one(True, False)»
@@ -143,32 +145,33 @@ are concerned:
143
145
my $j = +any "not a number", "42", "2.1";
144
146
(gather $j».take).grep(Numeric).say; # OUTPUT: «(42 2.1)»
145
147
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 > :
154
156
155
157
my $j = +any "not a number", "42", "2.1";
156
158
try say $j == 42;
157
159
$! and say "Got exception: $!.^name()"; # OUTPUT: «Got exception: X::Str::Numeric»
158
160
159
161
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:
163
165
164
166
sub calc ($_) { die when 13 }
165
167
my $j = any 1..42;
166
168
say try calc $j; # OUTPUT: «Nil»
167
169
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:
172
175
173
176
sub calc ($_) { die when 13 }
174
177
my $j = any 1..42;
@@ -198,9 +201,10 @@ Defined as:
198
201
199
202
multi method Str(Junction:D:)
200
203
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.
204
208
205
209
= head2 method gist
206
210
0 commit comments