Comments (10)
There is already let i = if b {"Yes"} else {"No"};
from crab.
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.
omg yes
from crab.
?!
Brilliant. Just brilliant. Yes.
from crab.
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:
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.
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.
What if this was done with a macro?
let some_var = ?!(some_condition ? "true" : "false");
from crab.
There is already
let i = if b {"Yes"} else {"No"};
And it would literally just expand to this
from crab.
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 Option
s 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.
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)
- Cargo HOT 2
- Fungal references in repository should be replaced with crab HOT 1
- approach to renaming should be more robust
- need to rename in public facing files HOT 3
- create experimental branch HOT 1
- you're probably infringing the Rust trademark by using their logo HOT 8
- Can the knife in the logo be removed/changed? HOT 3
- Automated updates from the fungus repo through GH-Actions HOT 1
- Add ability to ignore tests at runtime HOT 2
- whatβs the difference and relationship between crab and rust? HOT 3
- Feature: `crabup.sh` for installing crab lang HOT 1
- Custom crabc as dependency in Cargo.toml HOT 3
- Read 1st: What is Where ?
- Isn't it time to archive this project? HOT 8
- `break rust` error message doesn't work properly HOT 2
- Will coroutines with stacks be supported?Virtual thread or Green thread
- Rename `rustup` into `carcinise` HOT 1
- Is crab lang dead? HOT 3
- Actually we can use this repo as a experimental field HOT 1
- Why everything keeps evolving into crabs? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from crab.