[英]Type mismatch in scala with wildcards
我有一段Java代码,正试图在Scala中实现。
Java代码,工作正常。
protected byte[] serialize(final TBase<?, ?> base) {
try {
return new TSerializer(new TCompactProtocol.Factory()).serialize(base);
} catch (final TException e) {
throw new RuntimeException(e);
}
}
我想实现的scala代码。
def serialize(base: TBase[_,_]): Array[Byte]={
try{
return new TSerializer(new TCompactProtocol.Factory()).serialize(base)
}catch {
case ex: TException => {
throw new RuntimeException(ex.getMessage())
}
}
}
编译器没有给我"type mismatch; found : org.apache.thrift.TBase[_$1,_$2] where type _$2, type _$1 required: org.apache.thrift.TBase[_ <: org.apache.thrift.TBase[_, _], _ <: org.apache.thrift.TFieldIdEnum]"
的错误"type mismatch; found : org.apache.thrift.TBase[_$1,_$2] where type _$2, type _$1 required: org.apache.thrift.TBase[_ <: org.apache.thrift.TBase[_, _], _ <: org.apache.thrift.TFieldIdEnum]"
我到处看了很多,建议之一是使用,有些则没有。 但不确定这样做是否正确。 有人可以帮我弄这个吗。
没有更多信息,此答案只是基于错误的猜测:
type mismatch;
found : TBase[_$1,_$2] where type _$2, type _$1
required: TBase[_ <: TBase[_, _], _ <: TFieldIdEnum]
将类型参数约束为错误消息,可能会解决此问题:
def serialize(base: TBase[_ <: TBase[_,_] ,_ <: TFieldIdEnum])
如果不查看序列化方法和TBase类的签名,很难说出为什么会这样。 我只能推测java通配符带来了已经在其他地方定义的约束,而scala existencials没有,要求您声明而不是明确声明。
另外,请勿使用return
关键字。 它没有按照您的想法做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.