1. 12

  2. 9

    One of the many artificial problems that C creates.

    1. 6

      I’m glad all the new languages have switched (Rust, Swift too). It’s the same amount of typing:

      if (cond) then
      if cond {then}

      It’s easier to parse. Avoids “goto fail” errors. After getting used to it, it also feels more consistent: code attached to a construct is always a block, and expressions never need unnecessary parens.

      1. 8

        Rust has its own version of the problem, that we could call the “brace association” problem. Basically, you have to decide if Symbol is a struct name, and thus Symbol { ... } is a struct literal, or if it’s just a variable name.

        if Symbol { whatever } { whatever }

        Rust goes with “it’s a variable name”, by fiat*, but that’s how the dangling else was solved, too.

        * since a struct literal cannot be of type bool, it’s not an arbitrary choice, but just because it can’t possibly type check doesn’t mean it can’t be parsed.

        1. 2

          Nice, I didn’t realize this ambiguity existed. For humans, it helps that types are upper case and variables are lower case, but that’s by convention so it doesn’t help the parser much. Also, I am again slightly baffled by the amount of work rustc puts into weird edge cases:

             Compiling playground v0.0.1 (/playground)
          error: struct literals are not allowed here
           --> src/main.rs:8:8
          8 |     if foo { i: 3 } {
            |        ^^^^^^^^^^^^
          help: surround the struct literal with parentheses
          8 |     if (foo { i: 3 }) {
            |        ^            ^
      2. 4

        This always seemed like an artificial problem that is created in misguided attempts at terseness.

        For JS, to take one example, is this:

        if (imABoolean) { runThis() }

        so much worse than

        if (imABoolean) runThis()

        Just make the block markers required with no exceptions. It’s not such a heavy burden.

        And to immediately break my own rule, I really like conditional suffixes, as in Ruby and others:

        runThis() if imABoolean

        1. 2

          I think the Ruby style is a decent compromise. You don’t have to write a multi-line-style block for a single conditional. But you also can’t do anything too confusing with it, since it only supports the single expression, and if you want to add an else or more lines, you have to switch to the more conventional multi-line syntax.

        2. 1

          This is also famously an issue in OCaml syntax: https://github.com/ocaml/ocaml/issues/9034