-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Strengthen overloading resolution to deal with extension methods #6116
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 all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6dbf71d
Allow for overloaded extensions in hasExtensionMethod
odersky e8d1b19
Strengthen overloading resolution to deal with extension methods
odersky f611dbf
Fix signature help test
odersky 1d09d41
Fix advanced candidate setup
odersky c710d81
More user friendly printing of prototypes
odersky 6909021
Improve error message for ambiguous extension methods
odersky 1762d21
Implement overriding checks for extension methods
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
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
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,14 @@ | ||
object Test { | ||
implied A { | ||
def (x: Int) |+| (y: Int) = x + y | ||
} | ||
implied B { | ||
def (x: Int) |+| (y: String) = x + y.length | ||
} | ||
assert((1 |+| 2) == 3) // error ambiguous | ||
|
||
locally { | ||
import B.|+| | ||
assert((1 |+| "2") == 2) // OK | ||
} | ||
} |
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,8 @@ | ||
class A { | ||
def f(x: Int)(y: Int): Int = 0 | ||
def (x: Int) g (y: Int): Int = 1 | ||
} | ||
class B extends A { | ||
override def (x: Int) f (y: Int): Int = 1 // error | ||
override def g(x: Int)(y: Int): Int = 0 // error | ||
} |
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,122 @@ | ||
object Test extends App { | ||
// warmup | ||
def f(x: Int)(y: Int) = y | ||
def f(x: Int)(y: String) = y.length | ||
assert(f(1)(2) == 2) | ||
assert(f(1)("two") == 3) | ||
|
||
def g[T](x: T)(y: Int) = y | ||
def g[T](x: T)(y: String) = y.length | ||
assert(g[Int](1)(2) == 2) | ||
assert(g[Int](1)("two") == 3) | ||
assert(g(1)(2) == 2) | ||
assert(g(1)("two") == 3) | ||
|
||
def h[T](x: T)(y: T)(z: Int) = z | ||
def h[T](x: T)(y: T)(z: String) = z.length | ||
assert(h[Int](1)(1)(2) == 2) | ||
assert(h[Int](1)(1)("two") == 3) | ||
assert(h(1)(1)(2) == 2) | ||
assert(h(1)(1)("two") == 3) | ||
|
||
// Test with extension methods in implied object | ||
object test1 { | ||
|
||
implied Foo { | ||
def (x: Int) |+| (y: Int) = x + y | ||
def (x: Int) |+| (y: String) = x + y.length | ||
|
||
def (xs: List[T]) +++ [T] (ys: List[T]): List[T] = xs ++ ys ++ ys | ||
def (xs: List[T]) +++ [T] (ys: Iterator[T]): List[T] = xs ++ ys ++ ys | ||
} | ||
|
||
assert((1 |+| 2) == 3) | ||
assert((1 |+| "2") == 2) | ||
|
||
val xs = List(1, 2) | ||
assert((xs +++ xs).length == 6) | ||
assert((xs +++ xs.iterator).length == 4, xs +++ xs.iterator) | ||
} | ||
test1 | ||
|
||
// Test with imported extension methods | ||
object test2 { | ||
import test1.Foo._ | ||
|
||
assert((1 |+| 2) == 3) | ||
assert((1 |+| "2") == 2) | ||
|
||
val xs = List(1, 2) | ||
assert((xs +++ xs).length == 6) | ||
assert((xs +++ xs.iterator).length == 4, xs +++ xs.iterator) | ||
} | ||
test2 | ||
|
||
// Test with implied extension methods coming from base class | ||
object test3 { | ||
class Foo { | ||
def (x: Int) |+| (y: Int) = x + y | ||
def (x: Int) |+| (y: String) = x + y.length | ||
|
||
def (xs: List[T]) +++ [T] (ys: List[T]): List[T] = xs ++ ys ++ ys | ||
def (xs: List[T]) +++ [T] (ys: Iterator[T]): List[T] = xs ++ ys ++ ys | ||
} | ||
implied Bar for Foo | ||
|
||
assert((1 |+| 2) == 3) | ||
assert((1 |+| "2") == 2) | ||
|
||
val xs = List(1, 2) | ||
assert((xs +++ xs).length == 6) | ||
assert((xs +++ xs.iterator).length == 4, xs +++ xs.iterator) | ||
} | ||
test3 | ||
|
||
// Test with implied extension methods coming from implied alias | ||
object test4 { | ||
implied for test3.Foo = test3.Bar | ||
|
||
assert((1 |+| 2) == 3) | ||
assert((1 |+| "2") == 2) | ||
|
||
val xs = List(1, 2) | ||
assert((xs +++ xs).length == 6) | ||
assert((xs +++ xs.iterator).length == 4, xs +++ xs.iterator) | ||
} | ||
test4 | ||
|
||
class C { | ||
def xx (x: Any) = 2 | ||
} | ||
def (c: C) xx (x: Int) = 1 | ||
|
||
val c = new C | ||
assert(c.xx(1) == 2) // member method takes precedence | ||
|
||
object D { | ||
def (x: Int) yy (y: Int) = x + y | ||
} | ||
|
||
implied { | ||
def (x: Int) yy (y: Int) = x - y | ||
} | ||
|
||
import D._ | ||
assert((1 yy 2) == 3) // imported extension method takes precedence | ||
|
||
trait Rectangle { | ||
def a: Long | ||
def b: Long | ||
} | ||
|
||
case class GenericRectangle(a: Long, b: Long) extends Rectangle | ||
case class Square(a: Long) extends Rectangle { | ||
def b: Long = a | ||
} | ||
|
||
def (rectangle: Rectangle) area: Long = 0 | ||
def (square: Square) area: Long = square.a * square.a | ||
val rectangles = List(GenericRectangle(2, 3), Square(5)) | ||
val areas = rectangles.map(_.area) | ||
assert(areas.sum == 0) | ||
} |
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.
What is the purpose of the dispatch here? Semantically it seems the same as:
Is it for performance? It seems we save several
exists
check this way.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.
Yes, it's for performance. We optimize for the common case where the member is not overloaded.