Hasty Briefsbeta

Bilingual

No semicolons needed: a survey of programming language syntaxes

8 hours ago
  • #semicolon
  • #syntax
  • #programming-languages
  • The article discusses the implementation of semicolon-less statement termination in programming languages, focusing on different approaches taken by languages like Python, Go, Kotlin, Swift, JavaScript, Gleam, Lua, R, Ruby, Julia, and Odin.
  • Python uses whitespace sensitivity and explicit line joining with backslashes or parentheses to delimit statements.
  • Go automatically inserts semicolons during lexing, with strict rules on where semicolons can be inserted, leading to a less flexible but simple approach.
  • Kotlin integrates newline handling directly into its grammar, allowing for nuanced control but at the cost of complexity.
  • Swift parses expressions greedily, ignoring newlines unless they lead to syntax errors, with some whitespace-sensitive rules to prevent ambiguity.
  • JavaScript's automatic semicolon insertion is complex and error-prone, leading to recommendations against relying on it.
  • Gleam and Lua parse expressions without considering whitespace, leading to unambiguous but potentially confusing code in some cases.
  • R and Ruby treat newlines as statement terminators unless the expression is incomplete, with Ruby adding special rules for method chaining.
  • Julia's approach is similar to R and Python, preferring to split statements at newlines unless the syntax requires continuation.
  • Odin mixes Python and Go's approaches, with semicolon insertion by the lexer but not within certain delimiters, and allows for flexible brace placement.
  • The article concludes with guidelines for designing semicolon-less syntax, emphasizing clear rules, simplicity, and tooling to catch mistakes.