Fix the handling of uninhabited types in pattern matching. by canndrew · Pull Request #1872 · rust-lang/rfcs (original) (raw)
Hmm, I just realized if one has a tuple of &!
, and only some of them are valid, punning on _
isn't so good. Let me amend my alternative so !
is a pattern that asserts the thing it is matching is "trivially" uninhabited.
Examples:
- unsafe {
match get(): &! {
&!, // no arm needed
}
} - unsafe {
match get(): Void {
!, // overkill, but accepted.
}
} - unsafe {
match get(): Result<usize, !> {
Ok(n) => println!("going strong {}", n);
Err(_) => println!("still alive!");
}
} - unsafe {
match get(): Result<(usize, !), (Void, !)> {
Ok((n, )) => println!("still alive! {}", n);
Err((, !)), // only 1!
needed
}
}
This is great for those coming from Haskell [haha, what a pedagogical priority], because if you really really drunkenly squint, asserting uninhabitedness is like forcing a binding (with !), and and @arielb1's trap representations are like the bottom value.