reasonml / reason

Simple, fast & type safe code that leverages the JavaScript & OCaml ecosystems
http://reasonml.github.io
MIT License
10.13k stars 430 forks source link

Confusing semicolons #1338

Closed mrkaspa closed 3 months ago

mrkaspa commented 7 years ago

Semicolons should work similar to ocaml just when an instruction has side effects, I don't get yet why to put a semicolon at the end of }, this was used in ocaml before to separate the functions and was double ;; but now you don't have to and it's better, also semicolons should have a section on the reasonml documentation, also I used to code on JS and TS without semicolons and there are a lot of programmers that don't like them because it's a keystroke more per each line I think this should be improved on reasonml

TheSpyder commented 7 years ago

Coding without semicolons in JS (and I'm guessing TS too) is not actually coding without semicolons, it's coding with a weird set of rules the programmer must remember about where implicit semicolons will be added.

Types make Reason better than JS. Requiring semicolons makes it more consistent. As for OCaml, they're also replacing the word in that permeates a lot of my OCaml functions.

You're right, though, there should be a few notes about this in the docs.

mrkaspa commented 7 years ago

@TheSpyder I know that the semicolon rules are not so much but if let always ends with in in ocaml why not adding it automatically in the code generator and take the lastest expresion as the return type as in ruby, elixir or elm, personally I dislike to type a character per each line it is to put the work of the parser on the developer's shoulders

TheSpyder commented 7 years ago

Reason doesn't have in, it uses semicolons instead. Either way ocaml-style structure requires something to be there, whitespace doesn't matter so the compiler can't presume a let declaration is finished just because a newline character is reached.

Everything in syntax is about trade-offs. There are benefits to the developer of having this semicolon with reduced ambiguity. They just aren't very well documented.

As for expressions; the last expression in a function is indeed the return type. And in some cases, the semicolon can be left out (you still need at least one per function though). If you're using vscode the Format Document shortcut should work and will remove unnecessary semicolons.

mrkaspa commented 6 years ago

why if I make this

let sum = (a, b) => {
  let w = a + 1;
  let x = b + 1;
  w + x
};

the formatter auto insert the semicolon at the end line

let sum = (a, b) => {
  let w = a + 1;
  let x = b + 1;
  w + x;
};

I thouht semicolons worked similar that in ocaml and rust

jordwalke commented 6 years ago

They are parsed similar to OCaml and Rust, but they are printed in the final line. We made this change based on feedback that people were annoyed by the formatter removing a final semicolon because it made it more difficult for them to copy/paste lines of code.

We will be continuing to relax the necessity of semicolons in other areas where it makes sense, and where it doesn't prove to be too challenging. For example:

() => {
  let x = 0
  let y = 0;
  result
}

Would also be parsed, and printed with all the semicolons. I'm not entirely sure this will be worthwhile, but we will give it a shot.

gilbert commented 6 years ago

Maybe a good first step would be to remove semicolons after all ending curly brackets?