scala - 这种类型参数语法无法编译的原因是什么?

标签 scala generics types scala-2.10

说我有:

class Class[CC[A, B]]
class Thing[A, B <: Int]
class Test extends Class[Thing] // compile error here

我收到编译器错误:

kinds of the type arguments (cspsolver.Thing) do not conform to the expected kinds of the type parameters (type CC) in class Class. cspsolver.
Thing's type parameters do not match type CC's expected parameters: type C's bounds <: Int are stricter than type B's declared bounds >: Nothing <: Any



但是,当我修改代码时,它看起来像这样:
class Class[CC[A, B]]
class Thing[A, B] {
  type B <: Int
}
class Test extends Class[Thing]

它编译得很好。它们在功能上不是等效的吗?

最佳答案

原因在编译器消息中给出。在 Class你期望一个不受限制的CC , 而 Thing有第二个类型参数必须是 <: Int 的限制.一种可能性是将相同的约束添加到 Class

class Class[CC[A,B <: Int]]
class Thing[A, B <: Int]
class Test extends Class[Thing]

关于scala - 这种类型参数语法无法编译的原因是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18073160/

相关文章:

python - 如何在 Python 中比较对象的类型?

scala - 有人可以解释一下 SBT 的正确使用方法吗?

scala - 如何创建打印命令行参数的任务?

css - 在 gatling 中,如何验证通过 css 检查提取的字符串的值?

java - 如何在 Java 中实例化泛型方法参数的实例?

c# - 将空的 IEnumerable 参数传递给方法

c# - 泛型:如何检查 T 的确切类型,没有 T 的对象

java - long mod 操作返回 int Java

haskell - 在 Haskell 中跨应用程序是否保留了类型相等性?

scala - 如何在 sonatype 和 maven 中发布 scala 模块?