aboutsummaryrefslogtreecommitdiff
path: root/tests/run
diff options
context:
space:
mode:
Diffstat (limited to 'tests/run')
-rw-r--r--tests/run/structural.scala25
-rw-r--r--tests/run/structuralNoSuchMethod.check1
-rw-r--r--tests/run/structuralNoSuchMethod.scala23
3 files changed, 49 insertions, 0 deletions
diff --git a/tests/run/structural.scala b/tests/run/structural.scala
new file mode 100644
index 000000000..0f18f4579
--- /dev/null
+++ b/tests/run/structural.scala
@@ -0,0 +1,25 @@
+case class Record(elems: (String, Any)*) extends Selectable {
+ def selectDynamic(name: String): Any = elems.find(_._1 == name).get._2
+}
+
+object Test {
+ import scala.reflect.Selectable.reflectiveSelectable
+
+ def f(closeable: { def close(): Unit }) =
+ closeable.close()
+
+ type RN = Record { val name: String; val age: Int }
+
+ def g(r: RN) = r.name
+
+ val rr: RN = Record("name" -> "Bob", "age" -> 42).asInstanceOf[RN]
+
+ def main(args: Array[String]): Unit = {
+ f(new java.io.PrintStream("foo"))
+ assert(g(rr) == "Bob")
+
+ val s: { def concat(s: String): String } = "abc"
+ assert(s.concat("def") == "abcdef")
+ }
+}
+
diff --git a/tests/run/structuralNoSuchMethod.check b/tests/run/structuralNoSuchMethod.check
new file mode 100644
index 000000000..20576fc9c
--- /dev/null
+++ b/tests/run/structuralNoSuchMethod.check
@@ -0,0 +1 @@
+no such method
diff --git a/tests/run/structuralNoSuchMethod.scala b/tests/run/structuralNoSuchMethod.scala
new file mode 100644
index 000000000..476d7ed82
--- /dev/null
+++ b/tests/run/structuralNoSuchMethod.scala
@@ -0,0 +1,23 @@
+import scala.reflect.Selectable.reflectiveSelectable
+
+/** Demonstrates limitation of structural method dispatch (in Scala 2.x and dotty).
+ * The method must be defined at exactly the argument types given in the structural type;
+ * Generic instantiation is not possible.
+ */
+object Test {
+ type T = { def f(x: String, y: String): String }
+
+ class C[X] {
+ def f(x: X, y: String): String = "f1"
+ }
+
+ val x: T = new C[String]
+
+ def main(args: Array[String]) =
+ try println(x.f("", "")) // throws NoSuchMethodException
+ catch {
+ case ex: NoSuchMethodException =>
+ println("no such method")
+ }
+
+}