Cipherd@lemmy.ml to Programmer Humor@programming.dev · 11 hours agofunctionslemmy.mlimagemessage-square36linkfedilinkarrow-up1341arrow-down15file-text
arrow-up1336arrow-down1imagefunctionslemmy.mlCipherd@lemmy.ml to Programmer Humor@programming.dev · 11 hours agomessage-square36linkfedilinkfile-text
minus-squarecalcopiritus@lemmy.worldlinkfedilinkarrow-up6·9 hours agoThis can also be a side product for code blocks being expressions instead of statements. In rust for example they are, so it’s not rare to see functions like: fn add_one(x: i32) -> i32 { x+1 } This lets you do amazing things like: let x = if y < 0.0 { 0.0 } else { y } which is the same as x = y < 0.0 ? 0.0 : y But is much better for more complex logic. So you can forget about chaining 3-4 ternary operations in a single line.
This can also be a side product for code blocks being expressions instead of statements.
In rust for example they are, so it’s not rare to see functions like:
fn add_one(x: i32) -> i32 { x+1 }This lets you do amazing things like:
let x = if y < 0.0 { 0.0 } else { y }which is the same as
x = y < 0.0 ? 0.0 : yBut is much better for more complex logic. So you can forget about chaining 3-4 ternary operations in a single line.