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?

Thx!

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.

6 Likes

!! 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 {
            v
        } else {
            coalesce!( $($c),+ )
        }
    );
    ($($a:expr),+ ,) => (coalesce!($($a),+))
}

https://play.rust-lang.org/?version=beta&mode=debug&edition=2021&gist=6bf29ccecd575d0ce23342027ff23ec5

So that you write it as

coalesce! [
    get_from_cache(),
    compute() + the_fresh() - value_yourself(),
    fallback_config.the_default,
]

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

6 Likes

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.

4 Likes

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.

5 Likes

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.

2 Likes

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

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.