-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SI-6355 SI-7059 it is possible to overload applyDynamic
As our discussion at https://issues.scala-lang.org/browse/SI-6355 shows, it looks like it is possible to overload applyDynamic, even though a straightforward way is closed. This commit codifies the pattern proposed by @paulp and makes sure that it doesn’t break in the future.
- Loading branch information
Showing
7 changed files
with
54 additions
and
7 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains 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,7 @@ | ||
t6355a.scala:12: error: implementation restriction: applyDynamic cannot be overloaded except by methods with different numbers of type parameters, e.g. applyDynamic[T1](method: String)(arg: T1) and applyDynamic[T1, T2](method: String)(arg1: T1, arg2: T2) | ||
def applyDynamic(name: String)(x: Int): Int = 2 | ||
^ | ||
t6355a.scala:18: error: implementation restriction: applyDynamic cannot be overloaded except by methods with different numbers of type parameters, e.g. applyDynamic[T1](method: String)(arg: T1) and applyDynamic[T1, T2](method: String)(arg1: T1, arg2: T2) | ||
def applyDynamic[T1, T2](name: String)(x: String, y: T1, z: T2): Int = 3 | ||
^ | ||
two errors found |
File renamed without changes.
This file contains 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,11 @@ | ||
t6355b.scala:14: error: value applyDynamic is not a member of A | ||
error after rewriting to x.<applyDynamic: error>("bippy") | ||
possible cause: maybe a wrong Dynamic method signature? | ||
println(x.bippy(42)) | ||
^ | ||
t6355b.scala:15: error: value applyDynamic is not a member of A | ||
error after rewriting to x.<applyDynamic: error>("bippy") | ||
possible cause: maybe a wrong Dynamic method signature? | ||
println(x.bippy("42")) | ||
^ | ||
two errors found |
This file contains 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,17 @@ | ||
import scala.language.dynamics | ||
|
||
class A extends Dynamic { | ||
def selectDynamic(method: String): B = new B(method) | ||
} | ||
class B(method: String) { | ||
def apply(x: Int) = s"$method(x: Int) called with x = $x" | ||
def apply(x: String) = s"""$method(x: String) called with x = "$x"""" | ||
} | ||
|
||
object Test { | ||
def main(args: Array[String]): Unit = { | ||
val x = new A | ||
println(x.bippy(42)) | ||
println(x.bippy("42")) | ||
} | ||
} |
This file contains 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,2 @@ | ||
bippy(x: Int) called with x = 42 | ||
bippy(x: String) called with x = "42" |
This file contains 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,17 @@ | ||
import scala.language.dynamics | ||
|
||
class A extends Dynamic { | ||
def applyDynamic(method: String): B = new B(method) | ||
} | ||
class B(method: String) { | ||
def apply(x: Int) = s"$method(x: Int) called with x = $x" | ||
def apply(x: String) = s"""$method(x: String) called with x = "$x"""" | ||
} | ||
|
||
object Test { | ||
def main(args: Array[String]): Unit = { | ||
val x = new A | ||
println(x.bippy(42)) | ||
println(x.bippy("42")) | ||
} | ||
} |