[Pre-RFC] Elvis/coalesce + ternary operator

Well, speaking of ?, we could spitball using the Try trait for A || B:

if let Ok(v) = Try::into_result(A) {
    Try::from_ok(v),
} else {
    B
}

This would be consistent with the current bool behaviour given an implementation like this:

struct True;
struct False;
impl Try for bool {
    type Success = True;
    type Error = False;
    fn into_result(self) -> Result<Self::Ok, Self::Error> {
        if self { Ok(True) } else { Err(False) }
    }
    fn from_error(False: Self::Error) -> Self { false }
    fn from_ok(True: Self::Ok) -> Self { true }
}

(Without saying whether that implementation would be a good idea.)

Would work for A && B too:

if let Err(v) = Try::into_result(A) {
    Try::from_error(v)
} else {
    B
}

That doesn't prohibit Some(1) && Some(2), though, which loses the 1 and is thus kinda weird. So people would probably rather try{(A?,B?)}, or similar, instead.

1 Like