What does “outer =>” really mean?

2020-02-01 04:14发布

问题:

Is there any documentation on the "outer =>" feature? It looks like a self type annotation with an infered type. However I have the feeling that I am wrong.

If it would be the case, is it only a different way to express access to super?

trait A extends (B => C) {
  outer =>
  def apply(x: B): C = outer(x)
}

回答1:

Not super, but the outer scope. It's a way to aliasing different scopes. For example:

class A(val x:Int) { thisA =>
 class B { 
   val x = 2 
   val y = x + thisA.x // without thisA how could we use A.x instead of B.x ? (*)
 }
}

There is a better illustration here.

(*) There exist another way to have the same effect, but it's beyond this question.



回答2:

It is a different way to access this. It is useful in cases where an outer this would be shadowed by another this in an inner class. That way, you can just give the outer this an additional (the original this would still be available when it is in scope so it’s not a renaming) name.



标签: scala