tokiwa-software / fuzion

The Fuzion Language Implementation
https://fuzion-lang.dev
GNU General Public License v3.0
48 stars 11 forks source link

`else` keyword in loops is confusing to beginners #3710

Open michaellilltokiwa opened 2 months ago

michaellilltokiwa commented 2 months ago

Maybe we should use another keyword like finally, or end?

At least it confused Simon, Khalil and me in the beginning.

simonvonhackewitz commented 2 months ago

I think finally or end could also be confusing, because that sounds as if it is always executed afterwards, but is actually only executed when until condition is not met. I don't have a better idea though.

michaellilltokiwa commented 2 months ago

@simonvonhackewitz hmm, yes.

fridis commented 2 months ago

I think our use of else is similar to that in Python, so millions can deal with this, even if Python does not have the counterpart of code following until. The only alternative I could think of is otherwise, but that has the same meaning as else.

michaellilltokiwa commented 2 months ago

fallback, or else might also be alternatives. I currently think all of them would be better than having two meanings for keyword else.

michaellilltokiwa commented 2 months ago

e.g.

    for
      res list u8 := nil, res ++ byte  # contains the decoded data at the end
      nxt := 0, nxt + 2
    while nxt < data.length
    do
      bits_0_3 := dec_char_at nxt
      bits_4_7 := dec_char_at nxt+1

      byte := if bits_0_3.ok && bits_4_7.ok
                [(bits_0_3.val << 4) | bits_4_7.val]
              else [u8 0]              # decoding result not used in error case

    until bits_0_3.is_error || bits_4_7.is_error
      if bits_0_3.is_error then bits_0_3.err
      else                  bits_4_7.err
    else
      outcome res.as_array

would be

    for
      res list u8 := nil, res ++ byte  # contains the decoded data at the end
      nxt := 0, nxt + 2
    while nxt < data.length
    do
      bits_0_3 := dec_char_at nxt
      bits_4_7 := dec_char_at nxt+1

      byte := if bits_0_3.ok && bits_4_7.ok
                [(bits_0_3.val << 4) | bits_4_7.val]
              else [u8 0]              # decoding result not used in error case

    until bits_0_3.is_error || bits_4_7.is_error
      if bits_0_3.is_error then bits_0_3.err
      else                  bits_4_7.err
    otherwise
      outcome res.as_array

Seems more clear to me with otherwise