News for hackers. Stuff that matters.
Re:Python with Braces by voidUpdate on Monday July 08, 2024 @03:32AM (#40903335)
I like semicolons, because it lets me use the brackets style I prefer (allman) instead of whatever the language devs think I should use. This is a really big issue for me trying to use Go, as they automatically insert a semicolon to the end of every line that doesn't end with a {, so the language forces you to use K&R, which I really dislike reading
Re:Python with Braces by xandrius on Monday July 08, 2024 @03:47AM (#40903400)
That's not really true though, I've used golang for years and never had semicolons added automatically.
Re:Python with Braces by voidUpdate on Monday July 08, 2024 @04:08AM (#40903506)
I don't mean it actually adds them to the file itself, its like it adds virtual ones during compilation. Try placing the { from the func main() { on a new line and it will fail to compile
Re:Python with Braces by xandrius on Monday July 08, 2024 @10:54AM (#40905978)
Oh, I see, that's what you meant. Thanks for clarifying :)
Re:Python with Braces by arp242 on Monday July 08, 2024 @12:24PM (#40906762)
The Go compiler inserts semi-colons. https://go.dev/ref/spec#Semicolons

If you follow those rules, you will see that this:

  if true;
  {
      fmt.Println()
  }
  else
  {
      fmt.Println()
  }
Will get rewritten to:

  if true;
  {
      fmt.Println();
  };
  else;
  {
      fmt.Println();
  };
And that won't work. That's why the braces need to be as "} else {". and "if .. {".

It used to be that the compiler gave some pretty confusing errors about semi-colons on this, but it seems that's been improved now.

JavaScript has similar semi-colon insertion by the way, but with some different (more confusing) rules.

Re:Python with Braces by xandrius on Monday July 08, 2024 @03:36PM (#40908708)
That's really neat, it had escaped me till now. Thanks for the explanation (today I'm one of the lucky 10,000!)