赫克托·斯卡拉类型不匹配
可能出什么问题了?
val is = IntegerSerializer.get
mutator.addInsertion(deviceId, COLUMN_FAMILY_CARSTATUS, createColumn("mileage", 111, ss, is))}
ModelOperation.scala:96: error: type mismatch;
[INFO] found : me.prettyprint.cassandra.serializers.IntegerSerializer
[INFO] required: me.prettyprint.hector.api.Serializer[Any]
[INFO] Note: java.lang.Integer <: Any (and me.prettyprint.cassandra.serializers.IntegerSerializer <: me.prettyprint.cassandra.serializers.AbstractSerializer[java.lang.Integer]), but Java-defined trait Serializer is invariant in type T.
[INFO] You may wish to investigate a wildcard type such as `_ <: Any`. (SLS 3.2.10)
[INFO] mutator.addInsertion(deviceId, COLUMN_FAMILY_CARSTATUS, createColumn("mileage", 111, ss, is))}
What could be wrong?
val is = IntegerSerializer.get
mutator.addInsertion(deviceId, COLUMN_FAMILY_CARSTATUS, createColumn("mileage", 111, ss, is))}
ModelOperation.scala:96: error: type mismatch;
[INFO] found : me.prettyprint.cassandra.serializers.IntegerSerializer
[INFO] required: me.prettyprint.hector.api.Serializer[Any]
[INFO] Note: java.lang.Integer <: Any (and me.prettyprint.cassandra.serializers.IntegerSerializer <: me.prettyprint.cassandra.serializers.AbstractSerializer[java.lang.Integer]), but Java-defined trait Serializer is invariant in type T.
[INFO] You may wish to investigate a wildcard type such as `_ <: Any`. (SLS 3.2.10)
[INFO] mutator.addInsertion(deviceId, COLUMN_FAMILY_CARSTATUS, createColumn("mileage", 111, ss, is))}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该错误表明
createColumn
需要Serializer[Any]
类型的序列化程序,但您传递的是Serializer[Integer]
类型的序列化程序。仅当Serializer
的类型参数是协变的(即定义为Serializer[+T]
)时,这才有效。但相反,Serializer
来自 Java,其中协方差的工作方式不同。Serializer[Integer]
类型可以安全地转换为Serializer[_ <: Any]
,因此 Scala 编译器建议也许createColumn
应该编写为期望不太具体的通配符类型。如果您无法修改
createColumn
,那么最后的办法是使用“类型系统逃生舱口”asInstanceOf
来转换为预期类型:The error is saying that
createColumn
requires a serializer of typeSerializer[Any]
, but you're passing one of typeSerializer[Integer]
. This would only work ifSerializer
were covariant in its type parameter (i.e., defined asSerializer[+T]
). But instead,Serializer
comes from Java, where covariance works differently.The type
Serializer[Integer]
can be safely cast toSerializer[_ <: Any]
, so the Scala compiler is suggesting that maybecreateColumn
should have been written to expect that less specific wildcard type instead.If you can't modify
createColumn
, then a last resort is to use the "type system escape hatch"asInstanceOf
to cast to the expected type: