As for if true && let (1,) = (2,) { }, it seems like we should expect boolean value at right hand side of &&.
So I guess we need a new binding syntax here.
what we can do now is to if let (true, (1,)) = (true, (2,)) { }, which is verbose.
so maybe if let (1,) = (2,) where true { } is good enough
and we might also introduce while let Some(x) = iter.next() where x > 42 { }
In case we need to match 2 patterns at the same time and deal with else-case in one place, maybe if let Some(x) = foo, Ok(y) = bar { ... } else { ... }, that is, use comma to separate multiple pattern-matching, but this syntax won't fit for boolean conditions.
so maybe combine these two if needed: if let Some(x) = foo, Ok(y) = bar where cond { ... } else { ... }
Instead of where, maybe when as a contextual keyword will look better
The usefulness of this pattern is most apparent in situations where:
You want the conditional to test a value that gets matched and bound in the same expression so you canāt use it yet.
Matching and binding it in an outer if let and then having a nested conditional creates a mess if you want to have else blocks that correspond to the else case of the āwholeā operation.
An example:
// Possible to have conditionals ATM:
if let (Some(inner), true) = (wrapped, local_var > 10) { ... }
// Not possible to have conditionals that depend on a value bound here:
// if let Some(inner) && inner > 10 { ... }
// So you nest the conditional:
if let Some(inner) = wrapped { if inner > 10 { ... } }
// But you lose the ability to do an else on the joint conditional and must duplicate:
if let Some(inner) = wrapped {
if inner > 10 { ... }
else { println!("Nope"); }
} else { println!("Nope"); }
// ...or use an auxillary variable
let mut nope = false;
if let Some(inner) = wrapped {
if inner > 10 { ... } else { nope = true; }
} else { nope = true; }
if nope { println!("Nope"); }
// Which both feel ugly and unergonomic :(
Yeah, Iāve been wanting to use all kinds of things:
&& or || initial match with further testing on the bound value
&& or || initial match with another match
else or else if with further matching
In general, it would be nice if the let match pattern behaved more like a common bool-yielding expression with the additional side effect of binding the matched object ā I guess this would depend on non-lexical lifetimes though.