5 ms·
Re: 'never-nesting', I'm not especially dogmatic, but I've never empirically seen a situation where this: match (condition_a, condition_b){ (true, true) =
by ToValueFunfetti 3y ago
Re: 'never-nesting', I'm not especially dogmatic, but I've never empirically seen a situation where this:
match (condition_a, condition_b){
(true, true) => fn_a()
(true, false) => fn_b()
(false, true) => fn_c()
(false, false) => fn_d()
}
isn't preferable to this:
if condition_a {
if condition_b {
fn_a()
} else {
fn_b()
}
else if condition_b {
fn_c()
} else {
fn_d()
}
(Assuming the syntax is available)
- makeitdouble 3y agoI think the lines gets blurred when doing early exits and guard conditions. For instance if !condition_a && !conditon_b { return fn_d() // probably an error condition ? } if condition_a && condition_b { return fn_a() } if condition_a { fn_b() } else { fn_c() }
- 1letterunixname 3y agoThese 3 examples optimize to the the same thing because they generate identical instructions: https://godbolt.org/z/a1Yq9rceE https://godbolt.org/z/a1Yq9rceE Note: Inverting and rearranging conditions changes what LLVM decides to output, sometimes for the worse. --opt-level=s is counterproductive here.
- titzer 3y agoI think I would like the former syntax. Witness one of the several nested matching situations I run into: https://github.com/titzer/wizard-engine/blob/master/src/engine/Type.v3#L272 https://github.com/titzer/wizard-engine/blob/master/src/engi... This would be much, much better if Virgil had pattern matching on tuples of ADTs.
- agumonkey 3y agoI really prefer the flat variant cause it helps me ensure exhaustiveness. In the end people probably trained their brain to read the nested variant to the point it's transparent to their neurons.
- cratermoon 3y agoOh no, hard disagree. The match implementation is far easier to reason about. I can see at a glance that if both condition_a and condition_b are true, call fn_a(). For the nested if version I have to trace each expression by hand.
- Izkata 3y agoThey said "isn't", not "is". Double negative. You two agree.
- cratermoon 3y agoUgh. I missed that. No wonder my grade school teachers taught us "don't use no double negatives".