aboutsummaryrefslogtreecommitdiff
path: root/tests/run/structuralNoSuchMethod.scala
blob: 476d7ed8225caac5893c4c38a3de63cbd1a7cc21 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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")
    }

}