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:

  1. unsafe {
    match get(): &! {
    &!, // no arm needed
    }
    }
  2. unsafe {
    match get(): Void {
    !, // overkill, but accepted.
    }
    }
  3. unsafe {
    match get(): Result<usize, !> {
    Ok(n) => println!("going strong {}", n);
    Err(_) => println!("still alive!");
    }
    }
  4. 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.