Git Product home page Git Product logo

Comments (10)

Iron-E avatar Iron-E commented on September 18, 2024 19

There is already let i = if b {"Yes"} else {"No"};

from crab.

Artoria2e5 avatar Artoria2e5 commented on September 18, 2024 4

The actual problem with ternary operator is that people can't agree on how to format it. Of course every serious person knows the one true way though:

c1 ? v1 :
c2 ? v2 :
c3 ? c31 ? v31 :
           v3d :
     vd;

from crab.

neriumrevolta avatar neriumrevolta commented on September 18, 2024 3

omg yes

from crab.

Artoria2e5 avatar Artoria2e5 commented on September 18, 2024 1

?!

Brilliant. Just brilliant. Yes.

from crab.

TCROC avatar TCROC commented on September 18, 2024

This will be in our "easy to use new super awesome gateway language" to introduce users from C#, Java, JavaScript, Python, etc. to the way of the crab:

#6

I don't have any responses on this one yet and it makes me sad :(. Lol. I actually believe in this one tho. I think it's the key ingredient to taking over the world.

Although we need a new name for it now. Crabby has been taken for clippy.

from crab.

Nekrolm avatar Nekrolm commented on September 18, 2024

Can we elaborate more on its integrarion with ? try operator

Let's we have

fn foo(x : Option<bool>) -> Option<S> {
     // should we use
     x?? Some(S) : None
     // or propagate unwrapping to Some branch
     x? (x? Some(S) : None) : None
     // or shorten
     x?? S
}

from crab.

TCROC avatar TCROC commented on September 18, 2024

What if this was done with a macro?

let some_var = ?!(some_condition ? "true" : "false");

from crab.

TCROC avatar TCROC commented on September 18, 2024

There is already let i = if b {"Yes"} else {"No"};

And it would literally just expand to this

from crab.

carterisonline avatar carterisonline commented on September 18, 2024

I know this is a pipe-dream which is why I didn't take my little "RFC" message seriously. I do think that rust crab needs more syntax sugar! Chances that this actually happens are basically none, but if we want to try, I have a good starting-point on how to actually implement this!

I suggest we change it from ? to ??, and from : to ?: in order to keep parity with the try operator and avoid breaking things in general. Here's how it would look:

let b = true;
let i = (b ?? "Yes") ?: "No";

In this snippet, ?? and ?: are both nonterminal, infix expressions the same way that +, -, etc. are. We don't have a builtin function for ??, so it should be implemented as a trait the same way that Add or Sub are:

// Impl for the `??` operator
impl<T> core::ops::IsTrue for bool {
    type Output = T;

    fn is_true(self, rhs: T) -> Option<Self::Output> {
        if self {
            Some(rhs)
        } else {
            None
        }
    }
}

For ?:, it would be easiest if we could implement the functionality already present in Option::unwrap_or, since IsTrue requires that an Option should be returned.

With both operators implemented, the equivalent "desugared" code from the first snippet would look like this:

let b = true;
let i = (if b {
    Some("Yes")
} else {
    None
}).unwrap_or("No");

There definitely could be a better way at implementing this, but using Options seems the most natural to me. If someone's also interested in digging through the compiler to make this work with me, I'll spend a full day trying to get this implemented if I have to! Sounds like a good exercise.

from crab.

ArjixWasTaken avatar ArjixWasTaken commented on September 18, 2024

If using the ? is out of the question, I don't think python's alternative for this is bad.

value if True else value2

yeah it is kinda the opposite of true ? value : value2

but still better than if true { value } else { value2 }

from crab.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.