by runevault · 2y ago · 0 comments
In languages where if is a statement (aka returns no value), you'd write code like
int value;
if(condition) { value = 5; } else { value = 10; }
Instead of just int value = if(condition) {5} else {10}
Some languages leave ifs as statements but add trinary as a way to get the same effect which is an acceptable workaround, but at least for me there are times I appreciate an if statement because it stands out more making it obvious what I'm doing.
int value1 = 0;
int value2 = 0;
if (condition) {
value1 = 8;
value2 = 16;
} else {
value1 = 128;
value2 = 256;
}
Would I have to repeat the if expression twice? int value1 = if (condition) { 8 } else { 128 };
int value2 = if (condition) { 16 } else { 256 };let (value1, value2) = if (condition) { (8, 16) } else { (16, 256) }
Or else you’d just use some other sort of compound value like a struct or something. Tuple is just convenient for doing it on the fly.
I love destructuring so much, I don't know if I'd want to use a language without it anymore.
So like I believe you can do this in Rust (haven't written it in a while, I know it has destructuring of tuples)
let (a, b) = if (condition) { (1, "hello") } else { (3,"goodbye") }
int value1 = 128;
int value2 = 256;
if (condition) {
value1 = 8;
value2 = 16;
}