-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #2570: Part 3 - The great () insert #2716
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
cc3a51d
Drop extraneous ()
odersky c8bdfbe
Fix nullary applications in collection strawman
odersky f7091cd
New nullary method fix to collection strawman.
odersky 244257b
Allow optional rewrite rule in testScala2Mode
odersky f3c6d91
Only add () to calls of nullary methods if they come from Java or Scala2
odersky 774c8da
Add () for nullary method applications
odersky ced54aa
Fix () insertion in xsbt
odersky 478a955
Fix inconsistent () handling in JavaEntity
odersky 0291b8f
Fix () errors in WikiParser
odersky 0777fa7
Fix algorithm bug in paramIndices
odersky 34bffbb
Fix now superfluous () in JavaConverterTest
odersky d8e4442
One more () error fixed
odersky 1f37b0c
Fix nullary overrides
odersky ded535d
Reject trailing `_` syntax under -strict mode.
odersky ea45332
Test cases for ()T/=>T overrides
odersky 416d290
Fix test
odersky 85efd24
Add reference docs
odersky ed69685
Fix test
odersky b7afd43
Fix typo
odersky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--- | ||
layout: doc-page | ||
title: "Automatic Eta Expansion" | ||
--- | ||
|
||
Previously, a method reference `m` was converted to a function value | ||
only if the expected type was a function type. If that was not the | ||
case, one had to write `m _` to force the conversion (which is called | ||
eta-expansion). | ||
|
||
For methods with one or more parameters, this restriction has now been | ||
dropped. Example: | ||
|
||
def m(x: Boolean, y: String)(z: Int): List[Int] | ||
val f1 = m | ||
val f2 = m(true, "abc") | ||
|
||
This creates two function values: | ||
|
||
f1: (Boolean, String) => Int => List[Int] | ||
f2: Int => List[Int] | ||
|
||
The syntax `m _` is no longer needed and will be deprecated in the | ||
future. | ||
|
||
Automatic eta expansion does not apply to "nullary" methods that take an empty parameter list. Given | ||
|
||
def next(): T | ||
|
||
, a simple reference to `next` does not auto-convert to a | ||
function. One has to write explicitely `() => next()` to achieve that | ||
(it's better to write it this way rather than `next _` because the latter | ||
will be deprecated). | ||
|
||
The reason for excluding nullary methods from automatic eta expansion | ||
is that Scala implicitly inserts the `()` argument, which would | ||
conflict with eta expansion. Automatic `()` insertion is | ||
[limited](auto-apply.md) in Dotty, but the fundamental ambiguity | ||
remains. | ||
|
||
### Reference | ||
|
||
For more info, see [PR #2701](https://github.com/lampepfl/dotty/pull/2701). | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
--- | ||
layout: doc-page | ||
title: "Auto-Application" | ||
--- | ||
|
||
Previously an empty argument list `()` was implicitly inserted when | ||
calling a nullary method without arguments. E.g. | ||
|
||
def next(): T = ... | ||
next // is expanded to next() | ||
|
||
In Dotty, this idiom is an error. | ||
|
||
next | ||
^ | ||
missing arguments for method next | ||
|
||
In Dotty, the application syntax has to follow exactly the parameter | ||
syntax. 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 in `scala.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. | ||
|
||
### Migrating code | ||
|
||
Existing Scala code with inconsistent parameters can still be compiled | ||
in Dotty under `-language:Scala2`. When paired with the `-rewrite` | ||
option, the code will be automatcally rewritten to conforrm to Dotty's | ||
stricter checking. | ||
|
||
### Reference | ||
|
||
For more info, see [Issue #2570](https://github.com/lampepfl/dotty/issue/2570) and [PR #2716](https://github.com/lampepfl/dotty/pull/2716). | ||
|
||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo: conforrm -> conform.