这是用Java定义的接口:
public interface TBase<T extends TBase, F extends TFieldIdEnum>
当我尝试使用此接口为方法添加类型边界时,如下所示:
def test[TB[A <: TBase[_, _], B <: TFieldIdEnum] <: TBase[A, B]](x: TB[_, _]) = x
发生错误:
type T's bounds <: TBase are stricter than type A's declared bounds <: TBase[_, _]
那么我该如何表达类型边界
T <: TBase
在斯卡拉?
附加信息:
我使用的是 Scala 2.11.8,界面来自一个非常旧的 apache 节俭版本,节俭-0.5.0
经过多次尝试,到目前为止,唯一成功的方法是删除更高种类的类型参数:
def test[A <: TBase[_, _], B <: TFieldIdEnum](x: TBase[A, B]) = x
scalaVersion := "2.12.8"
libraryDependencies += "org.apache.thrift" % "libthrift" % "0.12.0"
法典
import org.apache.thrift.{TBase, TFieldIdEnum}
import scala.language.higherKinds
object App {
def test[TB[A <: TBase[_, _], B <: TFieldIdEnum] <: TBase[A, B]](x: TB[_, _]) = x
}
产生错误
Error:(5, 55) type arguments [A,B] do not conform to trait TBase's type parameter bounds [T <: org.apache.thrift.TBase[T,F],F <: org.apache.thrift.TFieldIdEnum]
def test[TB[A <: TBase[_, _], B <: TFieldIdEnum] <: TBase[A, B]](x: TB[_, _]) = x
它不完全是你的,而是相似的。
最简单的解决方法是
def test[TB[A <: TBase[A, B], B <: TFieldIdEnum] <: TBase[A, B]](x: TB[_, _]) = x
跟
scalaVersion := "2.11.8"
resolvers += "twitter-repo" at "http://maven.twttr.com"
libraryDependencies += "org.apache.thrift" % "libthrift" % "0.5.0"
我无法重现您的错误。
import org.apache.thrift.{TBase, TFieldIdEnum}
import scala.language.higherKinds
object App {
def test[TB[A <: TBase[_, _], B <: TFieldIdEnum] <: TBase[A, B]](x: TB[_, _]) = x
}
编译没有错误。