Description
The RFC for if let
was accepted with the rationale that it lowers the boilerplate and improves ergonomics over the equivalent match
statement in certain cases, and I wholeheartedly agree.
However, some cases still remain exceedingly awkward; an example of which is attempting to match the "class" of a record enum
variant, e.g. given the following enum:
enum Foo {
Bar(u32),
Baz(u32),
Qux
}
and an instance foo: Foo
, with behavior predicated on foo
not being Bar
and a goal of minimizing nesting/brace creep (e.g. for purposes of an early return), the only choice is to type out something like this:
// If we are not explicitly using Bar, just return now
if let Foo::Bar(_) = self.integrity_policy {
} else {
return Ok(());
}
// flow resumes here
or the equally awkward empty match
block:
// If we are not explicitly using Bar, just return now
match self.integrity_policy {
Foo::Bar(_) => return Ok(());
_ => {}
}
// flow resumes here
It would be great if this were allowed:
// If we are not explicitly using Bar, just return now
if !let Foo::Bar(_) = self.integrity_policy {
return Ok(());
}
// flow resumes here
or perhaps a variation on that with slightly less accurate mathematical connotation but far clearer in its intent (you can't miss the !
this time):
// If we are not explicitly using Bar, just return now
if let Foo::Bar(_) != self.integrity_policy {
return Ok(());
}
// flow resumes here
(although perhaps it is a better idea to tackle this from an entirely different perspective with the goal of greatly increasing overall ergonomics with some form of is
operator, e.g. if self.integrity_policy is Foo::Bar ...
, but that is certainly a much more contentious proposition.)