scala - 自动映射和 "implicit conversion must be more specific then Any Ref"错误

标签 scala

基于 activator-akka-cassandra示例我正在创建我自己的应用程序,它将数据保存到 cassandra 中。

我定义了一个数据模型如下

import play.api.libs.json.Json

case class Location(lat: Double, long: Double)
object Location {
  implicit def toLocation(lat: Double, long: Double) = Location(lat, long)
}

case class LocationWithTime(location: Location, time: Long)
object LocationWithTime {
  implicit def toLocationWithTime(location: Location, time: Long) = LocationWithTime(location, time)
}

case class Ping(pingUuid: String, senPhoneNumber: String, recPhoneNumber: Set[String], locationWithTime: LocationWithTime)
object Ping {
  implicit val LocationFormat = Json.format[Location]
  implicit val LocationWithTimeFormat = Json.format[LocationWithTime]
  implicit val PingFormat = Json.format[Ping]
}

不幸的是,应该保存数据的代码:
def insertPing(ping: Ping): Unit =
    session.executeAsync(insertPing.bind(ping.pingUuid, ping.senPhoneNumber, ping.recPhoneNumber,
      ping.locationWithTime.location.lat, ping.locationWithTime.location.long, ping.locationWithTime.time))

不编译并返回错误
Error:(24, 38) the result type of an implicit conversion must be more specific than AnyRef
      ping.locationWithTime.location.lat, ping.locationWithTime.location.long, ping.locationWithTime.time))
                                 ^

example data model 中的案例类扩展 AnyVal。基于 this我猜想在我的情况下也有类似的东西可以解决问题,但我不能这样做,因为 ValueClasses 只能有一个参数。
我该如何解决这个问题?

最佳答案

虽然我不熟悉 Cassandra,但我认为你的问题是 bind需要 Java Object s(即 AnyRef s)和 lat , 是 scala.Double (即 Java double),它不是 AnyRef .

您可以通过包装您的 scala.Double 来实现这一点。 s 在 java.lang.Double .

insertPing.bind(ping.pingUuid, ping.senPhoneNumber, ping.recPhoneNumber, new java.lang.Double(ping.locationWithTime.location.lat), new java.lang.Double(ping.locationWithTime.location.long), ping.locationWithTime.time)

关于scala - 自动映射和 "implicit conversion must be more specific then Any Ref"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36677677/

相关文章:

具有更高级类型的 Scala 类型投影

scala - 如何在Scaladoc中的Object类中引用val的名称?

scala - 在 Scala 中导入 avro 模式

mysql - 使用 Slick 从 Scala 连接到 AWS MySQL 数据库

scala - AssemblyKey用作什么,以及如何导入它们?

java - 使用 Stanford Topic Modeling Toolkit (TMT) 加载类时出错

scala - 让 Scala 解释器在解释调用之间忘记

scala - 比较 Scala 泛型函数中泛型类型的值

Scala Lazy Collection 有人可以解释以下行为

scala - Scala 中的 lambda 类型是什么?它们有什么好处?