In Python, Python has Union type, which is convenient when a method can accept multi types:
from typing import Union
def test(x: Union[str,int,float,]):
print(x)
if __name__ == '__main__':
test(1)
test('str')
test(3.1415926)
Raku probably doesn't have Union type as Python, but a where
clause can achieve a similar effect:
sub test(\x where * ~~ Int | Str | Rat) {
say(x)
}
sub MAIN() {
test(1);
test('str');
test(3.1415926);
}
I wander if Raku have a possibility to provide the Union type as Python?
# vvvvvvvvvvvvvvvvvvvv - the Union type doesn't exist in Raku now.
sub test(Union[Int, Str, Rat] \x) {
say(x)
}