What is the syntax to return a value from a CATCH phaser from a block which is not a Routine?
sub foo() {
<1 2 3>.map: -> $a {
die 'oops';
CATCH { default { 'foo' } }
}
}
sub bar() {
<1 2 3>.map: -> $a {
die 'oops';
CATCH { default { return 'bar' } }
}
}
say foo(); # (Nil, Nil, Nil)
say bar(); # Attempt to return outside of immediatelly-enclosing Routine (i.e. `return` execution is outside the dynamic scope of the Routine where `return` was used)
edit: Desired output is:
say baz(); # (baz baz baz)
The use case is map
ing a Seq
with a method which intermittently throws an exception, handling the exception within the block passed to map by returning a default value.
Return exits out of a function scope, but the way you are using it in bar()
there are two functions at play.
bar()
method itself.This means that your return is ambiguous (well, at least to some people) and the compiler will balk.
Without the "return" the value in foo()
is handled as a constant within the block, and the block returns Nil. This means that in foo()
you effectively avoided parsing the meaning of return
, effectively pushing a Nil
on the stack.
That's why you have 3 Nil
s in the captured output for foo()
. For bar()
it is unclear if you wished to terminate the execution of the bar()
routine on the first thrown exception or if you just wanted to pass 'bar'
back as the non-Nil
value the CATCH block pushed onto the stack.
The slightly modified version of your code
#!/bin/env perl6
sub foo() {
<1 2 3>.map: -> $a {
die 'oops';
}
CATCH { default { 'foo' } }
}
sub bar() {
<1 2 3>.map: -> $a {
die 'oops';
}
CATCH { default { return 'bar' } }
}
say foo();
say bar();
might make this a bit more clear. It's output is
[edwbuck@phoenix learn_ruby]$ ./ed.p6
Nil
bar
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With