开发者

hector scala type mismatch

开发者 https://www.devze.com 2023-04-01 05:27 出处:网络
What could be wrong? val is = IntegerSerializer.get 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, 开发者_JAVA百科createColumn("mileage", 111, ss, 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 ...)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号