Over my dead body!
Over my dead body!
Is writing if a { .. } else {} so much worse than if a then .. else .. end (Ruby requires then/end instead of {}.
Python's indentation based syntax (which I think is great) cleverly solves the problem of requiring explicit statement delimiters but it's also a reason why lambda: is limited to one-liners. There's no free lunch.
To Go's credit they did put a lot of effort and design thought into removing line noise from syntax (compared to C) by e.g. adding an automatic semi-colon insertion rule, eliminating braces around if argument etc.
Overall, while syntax is not as clean as Python's, it comes pretty damn close.
package main
import "fmt"
func blah() bool {
return false
}
func main() {
x := 5
if(blah()) {
x++;
}
fmt.Printf("%d\n", x)
}
(which prints 5) and then this: package main
import "fmt"
func blah() bool {
return false
}
func main() {
x := 5
if(blah())
{
x++;
}
fmt.Printf("%d\n", x)
}
(which prints 6). Looks like a major problem to me, and at any rate it's a far cry from having made real (or any) progress. Python _did_ get whitespace right.In any case, language syntax should always take into account the expectation of programmer communities. If you push syntax too far, you lose too many people.
When D was nascent, many people proposed various schemes for making semicolons optional. I think it's great D didn't fall for such.
The most popular languages are C, C++, Java and C# and Go's syntax is very similar to those. If anything, Python is the weird one in how it treats the whitespace and hence most likely to confuse people coming from other languages.
As to optional semicolon elision - what is the problem with that? They make the code cleaner looking and gofmt will remove them for you even if you forget due to C/Java reflexes. I don't see the downside of making them optional.
That truly speaks poorly of you.
OTOH, the problem can easily be fixed by requiring a statement to contain at least one token.
I agree with you that Python's syntax is cleaner but I would like to bring few novel syntax-related things that Go did that are not as commonly known and in some cases are improvements even over Python.
In general I don't like deep indentation and Go has few things to help keep indentation to a minimum.
1. defer statement
In Go you can say:
f := os.Open("myfile.dat", ...)
defer f.Close()
That guarantees that f.Close() will be called at function exit. In Python/Java/C# to get the same guarantee, you would have to use try/finally block, introducing additional indentation for the code using f.2. Syntax for defining methods on interfaces
func (p *MyClass) Method(int i) {
return p.x + i
}
The equivalent in Python would be:class MyClass(): def Method(i): return this.x + i
Again, less indentation.
3. panic()/recover() way of handling fatal errors also doesn't introduce additional indentation level like try/catch blocks.