From f722de7c6131b544345dbc6745b5d219de5831e7 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Mon, 2 May 2016 15:56:52 +0200 Subject: A test case for overloading/overriding interactions This showcases a tricky interaction between overloading and overriding. See discussion of #1240 for context. --- tests/run/i1240.scala | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 tests/run/i1240.scala (limited to 'tests') diff --git a/tests/run/i1240.scala b/tests/run/i1240.scala new file mode 100644 index 000000000..860806ba5 --- /dev/null +++ b/tests/run/i1240.scala @@ -0,0 +1,27 @@ +// A tricky case of overriding behavior +// Note: It would be acceptable if this produced an error instead. +// Bit testing this is tricky. +abstract class Base[T] { + def foo(x: T): String +} + +class C[T] extends Base[T] { + + def foo(x: D): String = "D foo" + def foo(x: T): String = "T foo" +} + +object Test { + def main(args: Array[String]) = { + val b1: Base[D] = new C[D] // which of the two foo's in C overrides the one in B? + assert(b1.foo(new D) == "T foo") + val b2: Base[D] = new C[D] {} + // In Java, this gives an error like this: + // methods foo(A) from C[D] and foo(String) from C[D] are inherited with the same signature + // But the analogous example with `b1` compiles OK in Java. + assert(b2.foo(new D) == "T foo") + } +} + +class D + -- cgit v1.2.3