sub slick()
{
if do-stuff
{
return IntStr.new: 1, "SomeValue";
}
else
{
return IntStr.new: 0, "Some error message";
}
}
my $result = slick;
if +$result
{
process( ~$result );
}
else
{
log-error( ~$result );
}
I would not do this though, as you can pass around Failures just like any data type which but which act as undefined values.
sub slick()
{
if do-stuff
{
return "SomeValue";
}
else
{
fail "Some error message";
}
}
with slick() # is it defined
-> $result {
process $result
}
else
-> $error {
log-error $error.exception.message
}
Actually I would make it so that log-error accepts Failure objects, so that the .exception.message can be removed.
proto sub log-error ( $ ) {*}
multi sub log-error ( Str $message ) {
say $message; # do the logging here
}
multi sub log-error ( Exception $exception ) {
samewith $exception.message
}
multi sub log-error ( Failure $failure ) {
samewith $failure.exception.message
}
You can also change out any method if you mixin a role.
# this has the same effect as `but False`
'Some error message' but role { method Bool ( --> False ) {} }
|