3 ms·
Honestly this is something I love about Kotlin/Rust/Go. You either have them, or you do not. There are no time-wasting silly arguments over their veracity... ju
by nobleach 4y ago
Honestly this is something I love about Kotlin/Rust/Go. You either have them, or you do not. There are no time-wasting silly arguments over their veracity... just because a language makes them optional.
- nequo 4y agoIn Rust, they are not optional though. The semicolon turns an expression into an expression statement.[1] [1] https://doc.rust-lang.org/reference/statements.html https://doc.rust-lang.org/reference/statements.html
- masklinn 4y ago> The semicolon turns an expression into an expression statement.[1] And more relevant, this has the effect of suppressing the expression's value when used in tail position of a block, by moving from the second to the first branch of the block expression grammar: https://doc.rust-lang.org/reference/expressions/block-expr.html https://doc.rust-lang.org/reference/expressions/block-expr.h... Which also explains why it sometimes doesn't work e.g. in non-block match arms, because they're not blocks, and thus don't accept statements.
- nobleach 4y agoRight, that's exactly what I'm saying. They're NOT optional in any of those languages. Some of them (Kotlin and Go) don't have them, but there is no argument.
- nequo 4y agoOh, I misunderstood. I don't know Go very well but they do seem optional there: $ cat with_semicolons.go $ cat without_semicolons.go package main package main import "fmt" import "fmt" func main() { func main() { var x = 3; var x = 3 x += 1; x += 1 fmt.Printf("%d\n", x); fmt.Printf("%d\n", x) } } $ go run with_semicolons.go $ go run without_semicolons.go 4 4 $ $
- Beltalowda 4y agoGo requires every line to end in semi-colons if you look at the EBNF in the specification, but the compiler will insert them for you. It's kind of a subtle detail, but it shows up in some places, for example the following being a compile error: if true { } else { } Because the semicolon is inserted when the last character on a line is "one of the operators and punctuation ++, --, ), ], or }" (among other cases, see https://go.dev/ref/spec#Semicolons https://go.dev/ref/spec#Semicolons). So you need to use the } else { style. In practice, you can just leave of the semicolons at the end of lines (gofmt will remove them) and it's as if semicolons are not required, but if you want to get all technical about it then they are required.