【发布时间】:2019-09-05 02:19:02
【问题描述】:
这是一个用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 中?
附加信息:
我使用的是 Scala 2.11.8,界面来自非常旧的 apache thrift 版本,thrift-0.5.0
经过多次尝试, 到现在为止唯一成功的就是去掉了更高种类的类型参数:
def test[A <: TBase[_, _], B <: TFieldIdEnum](x: TBase[A, B]) = x
【问题讨论】:
-
无法立即重现。
public interface TBase<T extends TBase, F> {}的javac和object Foo { def test[TB[A <: TBase[_, _], B] <: TBase[A, B]](x: TB[_, _]) = x }的scalac似乎编译没有错误。错误究竟来自哪里?此外,this here 有<?, ?>。 -
@AndreyTyukin 这可能取决于
libthrift的版本。您的链接是 0.9。在 0.12 中是public interface TBase<T extends TBase<T,F>, F extends TFieldIdEnum> extends Comparable<T>, TSerializable, Serializable。 -
@w0mTea 您的
libthrift和Scala 版本是什么? -
@DmytroMitin 我使用的是 Scala 2.11.8 和一个非常旧的 apache thrift 版本,thrift-0.5.0
-
@w0mTea 我更新了我的答案。使用这些版本,我无法重现您的错误。试试
sbt clean compile。A <: TBase[A, B], B <: TFieldIdEnum适合你吗?
标签: scala generics type-bounds