by ahci8e · 2y ago · 0 comments
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.
Plus side I dabble in f# which is so much more expressive.
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;
}