在Python中,Python有Union个类型,当一个方法可以接受多种类型时,这很方便:
from typing import Union
def test(x: Union[str,int,float,]):
print(x)
if __name__ == '__main__':
test(1)
test('str')
test(3.1415926)
Raku可能没有Python那样的联合类型,但where
子句可以实现类似的效果:
sub test(\x where * ~~ Int | Str | Rat) {
say(x)
}
sub MAIN() {
test(1);
test('str');
test(3.1415926);
}
我想知道Raku是否有可能提供Python的联合类型?
# vvvvvvvvvvvvvvvvvvvv - the Union type doesn't exist in Raku now.
sub test(Union[Int, Str, Rat] \x) {
say(x)
}