I don't think there is direct way to do that, because it would require some special (hypothetical) identifier thisMethod
. However, depending on your context, the following two ways to avoid the name shadowing might be possible:
(1) Replace anonymous class A
with implementing class:
case class AImpl(a: Int) extends A
def f(a : Int): A = AImpl(a)
(2) Define f
in an abstract trait and use a concrete 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)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…