News for hackers. Stuff that matters.
Re:Push ifs up and fors down by ToValueFunfetti on Wednesday November 15, 2023 @06:51PM (#38284071)
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)
Re:Push ifs up and fors down by makeitdouble on Wednesday November 15, 2023 @07:25PM (#38284340)
I 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()
  }
Re:Push ifs up and fors down by 1letterunixname on Wednesday November 15, 2023 @08:21PM (#38284777)
These 3 examples optimize to the the same thing because they generate identical instructions:

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.

Re:Push ifs up and fors down by titzer on Wednesday November 15, 2023 @07:55PM (#38284599)
I 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/engi...

This would be much, much better if Virgil had pattern matching on tuples of ADTs.

Re:Push ifs up and fors down by agumonkey on Wednesday November 15, 2023 @08:07PM (#38284681)
I 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.
Re:Push ifs up and fors down by cratermoon on Wednesday November 15, 2023 @09:29PM (#38285250)
Oh 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.
Re:Push ifs up and fors down by Izkata on Wednesday November 15, 2023 @11:25PM (#38285790)
They said "isn't", not "is". Double negative. You two agree.
Re:Push ifs up and fors down by cratermoon on Thursday November 16, 2023 @08:18PM (#38298020)
Ugh. I missed that. No wonder my grade school teachers taught us "don't use no double negatives".