Hey there,
Since the introduction of the “fewer braces” syntax (which I’m a fan of), there is an odd inconsistency in the language: unary functions can be called with whitespace syntax, but parens and commas are required to call a function of more than one parameter:
// unary function call
foo:
bar.can(be) ++ "a complex".expression
// binary function call
foo(
bar.can(be) ++ "a complex".expression,
baz
)
And the longer I’ve been using fewerBraces
, the more I feel that every such function call sticks out visually like a sore thumb, especially given that it’s actually possible to mix paren/comma parameter lists with fewerBraces
function calls:
foo(
bar:
baz ++ qux,
bla
)
// equivalent to foo(bar(baz ++ qux), bla)
If you find this beautiful, please don’t leave a comment
This is not a contrived case: such code often occurs for example in zio-test where a suite could be defined like so:
def spec =
suite("MySuite")(
test("first test"):
doTheThing(),
test("second test"):
doTheOtherThing()
)
It shouldn’t be too hard to fix this: we can already infer semicolons, so why not add a syntax that works the same way but infers commas rather than semicolons?
I’m going to use @
as a mock syntax, but of course that’s completely open to debate.
E. g.
foo @
bar
baz
// equivalent to foo(bar, baz)
var
assignments in Scala are expressions that evaluate to the Unit value, so the following could be read as a call to function foo
where the expressions passed to it are var
assignment expressions.
foo @
a = b
x = y
// could be read as foo((a = b), (x = y))
However both var
assignments and passing the Unit value to a function are relatively rare in idiomatic Scala code, so this interpretation isn’t particularly useful. Rather, this syntax should be read as a function call with named parameters: foo(a = b, x = y)
.
Do you feel the same way about multi-line parameter lists as I do? Is @
a good syntax or should we go for something else?