There’s a Scala 3 proposal to remove the addition of a missing empty argument list on nullary methods without arguments. For example, the following code would no longer compile:
def next(): T = ...
next // is expanded to next()
In Scala 3, the application syntax would need to follow exactly the parameter syntax. Next, I quote the rest of the article motivating this removal from the Dotty website:
Excluded from this rule are methods that are defined in Java or that override methods defined in Java. The reason for being more lenient with such methods is that otherwise everyone would have to write
xs.toString().length()
instead of
xs.toString.length
The latter is idiomatic Scala because it conforms to the uniform access principle . This principle states that one should be able to change an object member from a field to a non-side-effecting method and back without affecting clients that access the member. Consequently, Scala encourages to define such “property” methods without a
()
parameter list whereas side-effecting methods should be defined with it. Methods defined in Java cannot make this distinction; for them a()
is always mandatory. So Scala fixes the problem on the client side, by allowing the parameterless references. But where Scala allows that freedom for all method references, Dotty restricts it to references of external methods that are not defined themselves in Dotty.For reasons of backwards compatibility, Dotty for the moment also auto-inserts
()
for nullary methods that are defined in Scala 2, or that override a method defined in Scala 2. It turns out that, because the correspondence between definition and call was not enforced in Scala so far, there are quite a few method definitions in Scala 2 libraries that use()
in an inconsistent way. For instance, we find inscala.math.Numeric
def toInt(): Int
whereas
toInt
is written without parameters everywhere else. Enforcing strict parameter correspondence for references to such methods would project the inconsistencies to client code, which is undesirable. So Dotty opts for more leniency when type-checking references to such methods until most core libraries in Scala 2 have been cleaned up.Stricter conformance rules also apply to overriding of nullary methods. It is no longer allowed to override a parameterless method by a nullary method or vice versa . Instead, both methods must agree exactly in their parameter lists.
class A { def next(): Int } class B extends A { /*!*/ def next: Int // overriding error: incompatible type }
Methods overriding Java or Scala-2 methods are again exempted from this requirement.
The goal of this proposal would be to also provide rewrites to Scala 2.x users so that the previously valid behaviour is now migrated to the stricter checking.
There is a related discussion on this topic started by @lihaoyi here: Getting rid of use-site method-call-parens-count?
This proposal is open for discussion in the community and will be discussed in our next SIP meeting, where we’ll take all your feedback into account for the approval/dismissal of this feature.