I donβt think there is a direct way to do this because it will require some special (hypothetical) identifier thisMethod . However, depending on your context, the following two ways are possible to avoid name shadowing:
(1) Replace the anonymous class A with the implementation class:
case class AImpl(a: Int) extends A def f(a : Int): A = AImpl(a)
(2) Define f in an abstract attribute and use a specific implementation for it:
trait F { def f(a: Int): A } object FImpl extends F { def f(a0: Int): A = new A { val a = a0 } } def test(factory: F): A = factory.f(a = 33)
source share