赫克托·斯卡拉类型不匹配

发布于 2024-12-02 10:23:29 字数 786 浏览 3 评论 0原文

可能出什么问题了?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦年海沫深 2024-12-09 10:23:29

该错误表明 createColumn 需要 Serializer[Any] 类型的序列化程序,但您传递的是 Serializer[Integer] 类型的序列化程序。仅当 Serializer 的类型参数是协变的(即定义为 Serializer[+T])时,这才有效。但相反,Serializer 来自 Java,其中协方差的工作方式不同。

Serializer[Integer] 类型可以安全地转换为 Serializer[_ <: Any],因此 Scala 编译器建议也许 createColumn应该编写为期望不太具体的通配符类型。

如果您无法修改 createColumn,那么最后的办法是使用“类型系统逃生舱口”asInstanceOf 来转换为预期类型:

val is = IntegerSerializer.get.asInstanceOf[Serializer[Any]] // defeats type system
mutator.addInsertion(... is ...)

The error is saying that createColumn requires a serializer of type Serializer[Any], but you're passing one of type Serializer[Integer]. This would only work if Serializer were covariant in its type parameter (i.e., defined as Serializer[+T]). But instead, Serializer comes from Java, where covariance works differently.

The type Serializer[Integer] can be safely cast to Serializer[_ <: Any], so the Scala compiler is suggesting that maybe createColumn 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:

val is = IntegerSerializer.get.asInstanceOf[Serializer[Any]] // defeats type system
mutator.addInsertion(... is ...)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文