nbuilding/N-lang

Breaking change: Disallow newline before function call parentheses and member access brackets

SheepTester opened this issue · 0 comments

N 1 current allows this

let wow = print
  ("wow")

which prints wow.

However, permitting a newline before a function call's parentheses like can be quite surprising and limits the types of expressions we can make usable as statements. For example, we can't add await statements because this becomes ambiguous:

let wow = someFunction
(() |> createCmd)!

Languages with C-style syntax typically do allow newlines before function call parentheses, but it's fine because they also use semicolons to separate statements. Python, in contrast, does not allow newlines. This Python code has an indentation error:

print
  ("hi")

To call print but with the parentheses on a separate line in Python, one must add a backslash:

print \
  ("hi")

But it's much more conventional to instead keep the parenthesis on the previous line and the function argument on a new line:

print(
  "hi"
)

JavaScript has the same problem as N. When omitting semicolons, this code

const url = 'https://www.example.com/'
(async () => {
  console.log(await fetch(url).then(r => r.text()))
})()

gives a TypeError because "https://www.example.com/" is not a function. In StandardJS, typically we add a semicolon before the (:

const url = 'https://www.example.com/'
;(async () => {
  console.log(await fetch(url).then(r => r.text()))
})()

JavaScript is not exemplary behaviour. Do not be like JavaScript.

We should at least prohibit a newline between the function and the left parenthesis for a function call in N, as well as between the value and the left square bracket in a member access operation (if they weren't already, I'm not sure). Thus, these become disallowed:

let wow = print
  ("hi")

let item = list
  [1]