简体   繁体   English

Scala:将泛型类型参数约束到另一个参数中的类型

[英]Scala: Constraining a generic type parameter to a type within another parameter

I'm still trying to grasp the Scala language, so please bear with me and all my questions. 我还在努力掌握Scala语言,所以请耐心等待我和我的所有问题。

Can I reference an abstract type from one type parameter in the bound for another? 我可以从绑定中的一个类型参数引用另一个的抽象类型吗? For instance, is there a way in which the following code can be made to work? 例如,有没有一种方法可以使下面的代码工作? The essence of what I'm trying to achieve here is that the KEY for the MAP parameter of C should be the SomeType of the parameter T. 我在这里想要实现的本质是C的MAP参数的KEY应该是参数T的SomeType。

trait T1 { 
    type SomeType;
}

trait MyMap[KEY, VALUE] { ... }


class C { 
  def m[T <: T1, MAP <: MyMap[T.SomeType, Int]] { ... }
}    

你需要类型投影

def m[T <: T1, MAP <: MyMap[T#SomeType, Int]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM