通用类型参数

时间:2017-07-05 14:33:23

标签: scala types

我有以下特点:

trait F{
   type T
}

trait C[T]{
   def getF: F{ type T = //I want the outer T}
}

有没有办法在不引入帮助type变量并重命名类型参数的情况下引用泛型类型参数?

2 个答案:

答案 0 :(得分:2)

似乎在Dotty中你可以这样做(见http://dotty.epfl.ch/docs/internals/higher-kinded-v2.html):

trait C[type T]{ self =>
   def getF: F{ type T = self.T }
}

否则对于当前的scala版本,我不知道在引入帮助type变量方面做得更好是否可行:

trait C[T]{
   type A = T
   def getF: F{ type T = A }
}

答案 1 :(得分:1)

如果你不想打电话给所有类型T,你可以做

trait C[Foo] {
  def getF: F { type T = Foo }
}