I understand the difference between zero-parameter and parameterless methods, but what I don't really understand is the language design choice that made parameterless methods necessary.
Disadvantages I can think of:
- It's confusing. Every week or two there are questions here or on the Scala mailing list about it.
- It's complicated; we also have to distinguish between
() => X
and=> X
. - It's ambiguous: does
x.toFoo(y)
mean what it says, orx.toFoo.apply(y)
? (Answer: it depends on what overloads there arex
'stoFoo
method and the overloads onFoo
'sapply
method, but if there's a clash you don't see an error until you try to call it.) - It messes up operator style method calling syntax: there is no symbol to use in place of the arguments, when chaining methods, or at the end to avoid semicolon interference. With zero-arg methods you can use the empty parameter list
()
.
Currently, you can't have both defined in a class: you get an error saying the method is already defined. They also both convert to a Function0
.
Why not just make methods def foo
and def foo()
exactly the same thing, and allow them to be called with or without parentheses? What are the upsides of how it is?