If “trait provided postfix macros” were a thing, this’d probably be a good thing as (fake syntax)
macro Try::or_else!($self, $($f:tt)+) {
if let Ok(v) = $crate::ops::Try::into_result($self) {
v
} else {
$($f)+
}
}
fn main() {
assert_eq!( 4, 4 );
assert_eq!( Some(4).or_else!(2), 4 );
assert_eq!( None.or_else!(None).or_else!(None).or_else!(2), 2 );
assert_eq!( None.or_else!(None).or_else!(Some(3)).or_else!(2), 3 );
assert_eq!( Err(4).or_else!(2), 2 );
assert_eq!( Some(1).or_else!(unreachable!()), 1 );
assert_eq!( loop { Err(()).or_else!(break 4) }, 4 );
assert_eq!( loop { break Some(1).or_else!(break 4) }, 1 );
}
I definitely think that this operation is useful for Rust (I mean, we have it in method form for both main Try types), and that generalizing it like this is cool. The continuing comma support for the standalone coalesce! is nice but probably not desired for a postfix macro or operator.
Actually, what operator could this “upgrade” to? ?? is ? ? and should probably stay that (because Result<Result<_,_>,_> exists) (though I could be convinced otherwise if res.flatten()? could work for two distinct error types here). ?: is almost definitely required to stay ? followed by type ascription. What other operators remain that would make sense for this? @#$_&-+()/*"':;!?,.~`|•√π÷×¶∆£¢€¥^°={}\%©®™✓[]<> is a dump of all the main symbols on my phone keyboard (GBoard) (which definitely has more than any desktop keyboard), and none of those seem super appropriate.
[[[✓ as an alias for ? when]]]