Reverse question mark operator

Hi, I know all knows the "?" operator, but I was looking if is possible to have something like that, but in reverse, lets call it "¿"?

What Am'I looking is an operator that if is Ok or a Value, return the function, if not unpack the error or None to the variable.

Is any crate or feature that can do this? or would need to be implemented? if is the last case, where to start?


You'd use a macro to do this, as Rust doesn't allow for custom operators. If you really, actually want an ¿ operator, you'll need to design and write an RFC proposing the change, and then convince the core team to adopt it.

If this happens, I think it imperative that it be made a prefix operator.


!! no more optinos? also, do you think could be useful for other ppl?

I think is useful when you need to run several functions, until you get a result, but would need other use cases to propose it.

I would suggest using a macro that emphasizes this structure, rather than hiding it in short-circuiting.

The thing that makes ? great is that you can mostly ignore it when you're trying to understand what a function does. But that's very much not true of the proposed ¿, since if you ignore it then many of the paths look silly.

I would encourage, instead, something like this:

macro_rules! coalesce {
    ($a:expr) => ( $a );
    ($a:expr , $($c:expr),+) => (
        if let Some(v) = $a {
        } else {
            coalesce!( $($c),+ )
    ($($a:expr),+ ,) => (coalesce!($($a),+))

So that you write it as

coalesce! [
    compute() + the_fresh() - value_yourself(),

and emphasize before people start reading the details of the first one that what you're doing is trying alternatives until something works.


Because they are a fairly recent addition, are you aware of let-else statements? That's what I would use for the problem you are describing.


The let-else statement, is powerful, for the case of the book is a good use case, but I think if we need to use several functions to test, would be better the macro or a similar one.

If a function to read, needs several steps like the book, there could be good use the let-else, just to be able easily read the intentions of the code.

I think is a great idea, for things like this, instead look a pure code solution, a macro that is able to synthesize the concept :smiley:

Have you seen the ControlFlow enum? Its semantics are less specific, so it's useful in cases where shortcircuiting isn't necessarily an error. It also works with the ? operator.


If my history knowledge is correct, before the ? operator existed, Rust used a macro for it called try!. The point being, defining a macro works well for this sort of thing.


Note that the way you use the try! macro was completely different from the macro I'm proposing here. Yes, one could make a yrt! macro that just flips the match arms, and it'd be easy to do so, but I'm intentionally suggesting not doing that.

1 Like

23 posts were split to a new topic: Programming languages and keyboard layouts