TL;DR You've encountered what I'd call a WTF?!?: when Type and ...
fails to check the and
clause. This answer talks about what's wrong with the when
and how to fix it. I've written another answer that focuses on using where
with a signature.
If you want to stick with when
, I suggest this:
when (condition when Type) { ... } # General form
when (* > 10 when Int) { ... } # For your specific example
This is (imo) unsatisfactory, but it does first check the Type
as a guard, and then the condition if the guard passes, and works as expected.
"Is this right?"
No.
given $ch {
when Int and * > 10 { say 65}
}
This code says 65
for any given integer, not just one over 10
!
WTF?!? Imo we should mention this on Raku's trap page.
We should also consider filing an issue to make Rakudo warn or fail to compile if a when
construct starts with a compile-time constant value that's a type object, and continues with and
(or &&
, andthen
, etc), which . It could either fail at compile-time or display a warning.
Here's the best option I've been able to come up with:
when (* > 10 when Int) { say 65 }
This takes advantage of the statement modifier (aka postfix) form of when
inside the parens. The Int
is checked before the * > 10
.
This was inspired by Brad++'s new answer which looks nice if you're writing multiple conditions against a single guard clause.
I think my variant is nicer than the other options I've come up with in previous versions of this answer, but still unsatisfactory inasmuch as I don't like the Int
coming after the condition.
Ultimately, especially if/when RakuAST lands, I think we will experiment with new pattern matching forms. Hopefully we'll come up with something nice that provides a nice elimination of this wart.
Really? What's going on?
We can begin to see the underlying problem with this code:
.say for ('TrueA' and 'TrueB'),
('TrueB' and 'TrueA'),
(Int and 42),
(42 and Int)
displays:
TrueB
TrueA
(Int)
(Int)
The and
construct boolean evaluates its left hand argument. If that evaluates to False
, it returns it, otherwise it returns its right hand argument.
In the first line, 'TrueA'
boolean evaluates to True
so the first line returns the right hand argument 'TrueB'
.
In the second line 'TrueB'
evaluates to True
so the and
returns its right hand argument, in this case 'TrueA'
.
But what happens in the third line? Well, Int
is a type object. Type objects boolean evaluate to False
! So the and
duly returns its left hand argument which is Int
(which the .say
then displays as (Int)
).
This is the root of the problem.
(To continue to the bitter end, the compiler evaluates the expression Int and * > 10
; immediately returns the left hand side argument to and
which is Int
; then successfully matches that Int
against whatever integer is given
-- completely ignoring the code that looks like a guard clause (the and ...
bit).)
If you were using such an expression as the condition of, say, an if
statement, the Int
would boolean evaluate to False
and you'd get a false negative. Here you're using a when
which uses .ACCEPTS which leads to a false positive (it is an integer but it's any integer, disregarding the supposed guard clause). This problem quite plausibly belongs on the traps page.