4 ms·
> Another example is the sequencing syntax <expr> ; <expr> and productions with <expr> as the right-most symbol: let test1 b = if b then prin
by ubertaco 3y ago
> Another example is the sequencing syntax <expr> ; <expr> and productions with <expr> as the right-most symbol:
let test1 b =
if b then
print_string "1"
else
print_string "2"; print_string "3"
> Here print_string "3" is not a part of the if expression, so this function always prints “3”.
Well, yeah. Here's the equivalent Java code:
void test1(bool b) {
if (b) print_string("1");
else print_string("2");
print_string("3");
}
You've chosen to use a single-statement "else" form _without_ "bracketing" it with `begin` and `end`:
let test1 b =
if b then
print_string "1"
else begin
print_string "2";
print_string "3"
end
...which is equivalent to this Java code, where you _also_ would have to "bracket" the else-block:
void test1(bool b) {
if (b) print_string("1");
else {
print_string("2");
print_string("3");
}
}
You could argue that Java (and C, and C++, and Javascript, and...) has the same "ambiguous parsing problem", then -- except that it doesn't, and the author is just thrown off by `begin`/`end` as block delimiters instead of curly braces, which might be a sign that they've also never seen Ruby before, or Elixir, or Pascal, or....
- yodsanklai 3y agoIt's a non-issue IMHO. Just use parenthesis if you're unsure, then auto-format. You'll catch any issue that wan't caught by the typechecker by reading the diff.
- charlesetc 3y agoYeah auto format solves a lot of the syntax problems. Dangling else for instance is not at all ambiguous when you have automatic indentation.
- deleted 3y ago[deleted]
- yakubin 3y ago> You could argue that Java (and C, and C++, and Javascript, and...) has the same "ambiguous parsing problem", then -- except that it doesn't The author didn’t claim there’s any ambiguity in parsing, only ambiguity in reading as a human. And I would agree that all the languages you mentioned have the same problem. And this is a real problem as long as people use this form of if (which they do). New languages avoid this pitfall by mandating the use of braces, in return dropping the parens, and I think that’s great.
- electroly 3y agoAgreed except for the part at the end: Java, C, C++, and JavaScript definitely do have the "dangling else" problem! It's a manifestation of the same basic issue. Who is saying they don't? You just showed how they do! It is a real problem and they all have it. The takeaway here isn't that those languages don't have the issue. They do. It's just really common in language design, that's all; certainly not something to single out OCaml about. Apple's infamous "goto vulnerability" was directly caused by this language design issue in good old C. https://en.wikipedia.org/wiki/Dangling_else https://en.wikipedia.org/wiki/Dangling_else (just to show that this is something people commonly consider to be a problem with Java, C, C++, and JavaScript)
- ubertaco 3y ago>Java, C, C++, and JavaScript definitely do have the "dangling else" problem! It's a manifestation of the same basic issue. Who is saying they don't? You just showed how they do! It is a real problem and they all have it. Yeah, that's actually exactly my point: it's not some new weird novel horrible broken parsing problem that only OCaml has; rather, it's a common parsing rule that many languages (including Java, C, C++, and JavaScript -- and yes, OCaml) have.
- Arnavion 3y agoNow keep reading on to literally the next sentence. The article's point is that the `if-else` behavior is inconsistent with `match`. And based on the part about double quotes in comments, the parser seems to be fucked up something fierce in other ways too.