|
| 1 | +--- |
| 2 | +layout: doc-page |
| 3 | +title: "Auto-Application" |
| 4 | +--- |
| 5 | + |
| 6 | +Previously an empty argument list `()` was implicitly inserted when |
| 7 | +calling a nullary method without arguments. E.g. |
| 8 | + |
| 9 | + def next(): T = ... |
| 10 | + next // is expanded to next() |
| 11 | + |
| 12 | +In Dotty, this idiom is an error. |
| 13 | + |
| 14 | + next |
| 15 | + ^ |
| 16 | + missing arguments for method next |
| 17 | + |
| 18 | +In Dotty, the application syntax has to follow exactly the parameter |
| 19 | +syntax. Excluded from this rule are methods that are defined in Java |
| 20 | +or that override methods defined in Java. The reason for being more |
| 21 | +lenient with such methods is that otherwise everyone would have to |
| 22 | +write |
| 23 | + |
| 24 | + xs.toString().length() |
| 25 | + |
| 26 | +instead of |
| 27 | + |
| 28 | + xs.toString.length |
| 29 | + |
| 30 | +The latter is idiomatic Scala because it conforms to the _uniform |
| 31 | +access principle_. This principle states that one should be able to |
| 32 | +change an object member from a field to a non-side-effecting method |
| 33 | +and back without affecting clients that access the |
| 34 | +member. Consequently, Scala encourages to define such "property" |
| 35 | +methods without a `()` parameter list whereas side-effecting methods |
| 36 | +should be defined with it. Methods defined in Java cannot make this |
| 37 | +distinction; for them a `()` is always mandatory. So Scala fixes the |
| 38 | +problem on the client side, by allowing the parameterless references. |
| 39 | +But where Scala allows that freedom for all method references, Dotty |
| 40 | +restricts it to references of external methods that are not defined |
| 41 | +themselves in Dotty. |
| 42 | + |
| 43 | +For reasons of backwards compatibility, Dotty for the moment also |
| 44 | +auto-inserts `()` for nullary methods that are defined in Scala 2, or |
| 45 | +that override a method defined in Scala 2. It turns out that, because |
| 46 | +the correspondence between definition and call was not enforced in |
| 47 | +Scala so far, there are quite a few method definitions in Scala 2 |
| 48 | +libraries that use `()` in an inconsistent way. For instance, we |
| 49 | +find in `scala.math.Numeric` |
| 50 | + |
| 51 | + def toInt(): Int |
| 52 | + |
| 53 | +whereas `toInt` is written without parameters everywhere |
| 54 | +else. Enforcing strict parameter correspondence for references to |
| 55 | +such methods would project the inconsistencies to client code, which |
| 56 | +is undesirable. So Dotty opts for more leniency when type-checking |
| 57 | +references to such methods until most core libraries in Scala 2 have |
| 58 | +been cleaned up. |
| 59 | + |
| 60 | +Stricter conformance rules also apply to overriding of nullary |
| 61 | +methods. It is no longer allowed to override a parameterless method |
| 62 | +by a nullary method or _vice versa_. Instead, both methods must agree |
| 63 | +exactly in their parameter lists. |
| 64 | + |
| 65 | + class A { |
| 66 | + def next(): Int |
| 67 | + } |
| 68 | + class B extends A { |
| 69 | + /*!*/ def next: Int // overriding error: incompatible type |
| 70 | + } |
| 71 | + |
| 72 | +Methods overriding Java or Scala-2 methods are again exempted from this |
| 73 | +requirement. |
| 74 | + |
| 75 | +### Migrating code |
| 76 | + |
| 77 | +Existing Scala code with inconsistent parameters can still be compiled |
| 78 | +in Dotty under `-language:Scala2`. When paired with the `-rewrite` |
| 79 | +option, the code will be automatcally rewritten to conforrm to Dotty's |
| 80 | +stricter checking. |
| 81 | + |
| 82 | +### Reference |
| 83 | + |
| 84 | +For more info, see [Issue #2570](https://github.com/lampepfl/dotty/issue/2570) and [PR #2716](https://github.com/lampepfl/dotty/pull/2716). |
| 85 | + |
| 86 | + |
| 87 | + |
0 commit comments