gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 15:59:00 25 4
gpt4 key购买 nike

说我有:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com