gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:09:05 26 4
gpt4 key购买 nike

基于 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com