Extension methods in typeclasses are surprising

In fact, it is the case in Java:

package test;

public class A {
	public static Integer f(final Integer s) {
		return s;
	}
}

public class B {
	public static String f(final String s) {
		return s;
	}
}

import static test.A.f;
import static test.B.f;

public class Test {
	public static void main(final String args[]) {
		System.out.println(f(1));
		System.out.println(f("a"));
	}
}
// 1
// a
2 Likes

15.12.1 is where it says a simple method name can be introduced by one or more imports in Java. I don’t know if there is a specific reason that was excluded for Scala.

6.5.7.1 (“Simple method names”) has an example which demonstrates a recent change in Scala 3 to follow Java, where inherited no longer shadows a definition in enclosing scope; so maybe there is still room to evolve.

class Super { def f(s: String) = s * 2 }
class C { def f(i: Int) = "hi" * i ; def s = new Super { def test = f(42) } }
2 Likes
1 Like