参考封闭类型的类型参数

时间:2014-10-11 15:04:35

标签: scala scope type-parameter

是否可以从声明具有相同名称的类型的内部作用域引用外部作用域类型参数?

例如,可添加 s A 需要在已定义类型A的范围内进行细化:

trait Addable {
  type A
  def addTwo(x: A, y: A): A
}

implicit class GenericAddable[A](first: A) {
  def +(second: A)(implicit proxy: Addable { type A = ??? }) =
    proxy.addTwo(first, second)
}

1 个答案:

答案 0 :(得分:1)

很确定这是不可能的。请改为使用GenericAddable的类型参数的其他名称。

或者,或者,定义一个不同名称的私有别名:

implicit class GenericAddable[A](first: A) {
  private type OuterA = A
  def +(second: A)(implicit proxy: Addable { type A = OuterA }) =
    proxy.addTwo(first, second)
}