I'd like to make a function that returns either a Range
or an any Junction
made up of multiple Ranges
.
Eg :
sub foo (Range $a, Range $b) {
if $a.min <= $b.max && $b.min <= $a.max {
($a.min < $b.min ?? $a.min !! $b.min)..($a.max < $b.max ?? $a.max !! $b.max)
} else {
($a|$b)
}
}
Is there an easy way of adding a Type constraint to the sub to say it could return a Range
or a Junction
?
Thought's I've had include
Any
.But if there's a simpler way that someone can think of.
Just create a subset that accommodates both results, and use it as if it were a type.
Note that since a Junction is not a subtype of Any, you have to mark it as being Mu.
(Junction specifically can't be Any and work the way it does)
my subset Range-or-Junction of Mu where Range|Junction;
proto sub foo ( Range, Range --> Range-or-Junction ) {*}
multi sub foo (Range $a,Range $b where $a.min ~~ $b || $a.max ~~ $b --> Range){
# note that this is wrong as it doesn't takes into consideration
# :excludes-min or :excludes-max
min($a.min,$b.min) .. max($a.max,$b.max)
}
multi sub foo (Range $a,Range $b --> Junction){
$a | $b
}
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