I want to check if all elements of an Array have given type.
$ raku -e 'my @t = 1,2,3; say all(@t) ~~ Int'
True
$ raku -e 'my @t = 1,2,3,"a"; say all(@t) ~~ Int'
False
Works as expected so far. Now I want to allow two types:
$ raku -e 'my @t = 1,2,3,"a"; say all(@t) ~~ Int|Str'
False
Why is so? If 1 ~~ Int|Str is True for single element why does it fail for all() elements junction?
BTW: This question is about understanding Junction ~~ Junction behavior (which is also a bit undocumented), not about alternative way of performing check from example (which I know is possible).
A few additional lines may help clarify what's going on:
say all(1, 2, 3) ~~ Int|Str; # OUTPUT: «True»
say all('a', 'b', 'c') ~~ Int|Str; # OUTPUT: «True»
say all(1, 2, 'c') ~~ Int|Str; # OUTPUT: «False»
That is, all(1, 2, 'c') ~~ Int|Str is asking "is it the case that all of 1, 2, 'c' are Ints or, alternatively, is it the case that all of 1, 2, 'c' are Strs?" Since neither of those is the case, it returns False.
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