Skip to content

Commit 431e19f

Browse files
committed
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.
1 parent 9f0594c commit 431e19f

File tree

7 files changed

+54
-7
lines changed

7 files changed

+54
-7
lines changed

test/files/neg/t6355.check

Lines changed: 0 additions & 7 deletions
This file was deleted.

test/files/neg/t6355a.check

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
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)
2+
def applyDynamic(name: String)(x: Int): Int = 2
3+
^
4+
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)
5+
def applyDynamic[T1, T2](name: String)(x: String, y: T1, z: T2): Int = 3
6+
^
7+
two errors found
File renamed without changes.

test/files/neg/t6355b.check

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
t6355b.scala:14: error: value applyDynamic is not a member of A
2+
error after rewriting to x.<applyDynamic: error>("bippy")
3+
possible cause: maybe a wrong Dynamic method signature?
4+
println(x.bippy(42))
5+
^
6+
t6355b.scala:15: error: value applyDynamic is not a member of A
7+
error after rewriting to x.<applyDynamic: error>("bippy")
8+
possible cause: maybe a wrong Dynamic method signature?
9+
println(x.bippy("42"))
10+
^
11+
two errors found

test/files/neg/t6355b.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import scala.language.dynamics
2+
3+
class A extends Dynamic {
4+
def selectDynamic(method: String): B = new B(method)
5+
}
6+
class B(method: String) {
7+
def apply(x: Int) = s"$method(x: Int) called with x = $x"
8+
def apply(x: String) = s"""$method(x: String) called with x = "$x""""
9+
}
10+
11+
object Test {
12+
def main(args: Array[String]): Unit = {
13+
val x = new A
14+
println(x.bippy(42))
15+
println(x.bippy("42"))
16+
}
17+
}

test/files/run/t6355.check

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bippy(x: Int) called with x = 42
2+
bippy(x: String) called with x = "42"

test/files/run/t6355.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import scala.language.dynamics
2+
3+
class A extends Dynamic {
4+
def applyDynamic(method: String): B = new B(method)
5+
}
6+
class B(method: String) {
7+
def apply(x: Int) = s"$method(x: Int) called with x = $x"
8+
def apply(x: String) = s"""$method(x: String) called with x = "$x""""
9+
}
10+
11+
object Test {
12+
def main(args: Array[String]): Unit = {
13+
val x = new A
14+
println(x.bippy(42))
15+
println(x.bippy("42"))
16+
}
17+
}

0 commit comments

Comments
 (0)