评估为 Any 的方法声明中的 Case class(带有伴生对象)
Case class (with companion object) in method declaration evaluated as Any
我对案例 class + 伴随对象及其(可能)与 Play 的交互有疑问。每当我尝试将它作为方法的参数传递时,它都被解释为类型 any
而不是 EventList
。但是,在方法体中使用它效果很好。
我似乎不明白为什么。下面是相关代码的简化部分(它来自大型代码库)。
EventList.scala:
package v1.objects
final case class EventList( ... ) {
...
}
object EventList {
def apply( ... ): EventList = {
...
new EventList( ... )
}
}
ObjectRepository.scala:
package v1.objects
class ObjectExecutionContext @Inject()(actorSystem: ActorSystem) extends CustomExecutionContext(actorSystem, "repository.dispatcher")
trait ObjectRepository {
def patch(newEvents: EventList)(implicit mc: MarkerContext): Future[Int]
...
}
@Singleton
class ObjectRepositoryImpl @Inject()()(implicit ec: ObjectExecutionContext) extends ObjectRepository {
override def patch(newEvents: EventList)(implicit mc: MarkerContext): Future[Int] = {
...
var eventList = EventList(...) // Retreive the old events from DB, works fine
eventList = EventList(eventList.underlying :+ newEvents.underlying) // <- This fails! newEvents has type Any
...
}
}
编译时的错误信息:
Error: overloaded method value apply with alternatives:
(underlying: List[v1.objects.Event])v1.objects.EventList
<and>
(doc: org.mongodb.scala.Document)v1.objects.EventList
cannot be applied to (String, List[Object])
eventList = EventList(eventList.underlying :+ newEvents.underlying)
这将创建一个列表:
eventList.underlying :+ newEvents.underlying
这会将列表作为元素添加到现有列表。
然后常见的Super-Type
就是Any
.
你需要的是将一个 List 添加到另一个 List 的函数 > 这个 returns 它们内容的列表:
eventList.underlying ++ newEvents.underlying
确切的语法取决于 underlying
类型。
示例:
case class EventList(underlying: Seq[String])
val el1 = EventList(Seq("e1", "e2"))
val el2 = EventList(Seq("e4", "e5"))
println(el1.underlying :+ el2.underlying) // List(e1, e2, List(e4, e5))
println(el1.underlying ++ el2.underlying) // List(e1, e2, e4, e5)
我对案例 class + 伴随对象及其(可能)与 Play 的交互有疑问。每当我尝试将它作为方法的参数传递时,它都被解释为类型 any
而不是 EventList
。但是,在方法体中使用它效果很好。
我似乎不明白为什么。下面是相关代码的简化部分(它来自大型代码库)。
EventList.scala:
package v1.objects
final case class EventList( ... ) {
...
}
object EventList {
def apply( ... ): EventList = {
...
new EventList( ... )
}
}
ObjectRepository.scala:
package v1.objects
class ObjectExecutionContext @Inject()(actorSystem: ActorSystem) extends CustomExecutionContext(actorSystem, "repository.dispatcher")
trait ObjectRepository {
def patch(newEvents: EventList)(implicit mc: MarkerContext): Future[Int]
...
}
@Singleton
class ObjectRepositoryImpl @Inject()()(implicit ec: ObjectExecutionContext) extends ObjectRepository {
override def patch(newEvents: EventList)(implicit mc: MarkerContext): Future[Int] = {
...
var eventList = EventList(...) // Retreive the old events from DB, works fine
eventList = EventList(eventList.underlying :+ newEvents.underlying) // <- This fails! newEvents has type Any
...
}
}
编译时的错误信息:
Error: overloaded method value apply with alternatives:
(underlying: List[v1.objects.Event])v1.objects.EventList
<and>
(doc: org.mongodb.scala.Document)v1.objects.EventList
cannot be applied to (String, List[Object])
eventList = EventList(eventList.underlying :+ newEvents.underlying)
这将创建一个列表:
eventList.underlying :+ newEvents.underlying
这会将列表作为元素添加到现有列表。
然后常见的Super-Type
就是Any
.
你需要的是将一个 List 添加到另一个 List 的函数 > 这个 returns 它们内容的列表:
eventList.underlying ++ newEvents.underlying
确切的语法取决于 underlying
类型。
示例:
case class EventList(underlying: Seq[String])
val el1 = EventList(Seq("e1", "e2"))
val el2 = EventList(Seq("e4", "e5"))
println(el1.underlying :+ el2.underlying) // List(e1, e2, List(e4, e5))
println(el1.underlying ++ el2.underlying) // List(e1, e2, e4, e5)