【发布时间】:2016-01-18 11:42:47
【问题描述】:
鉴于此代码:
object Testy extends App {
case class Person(
id: Option[Long],
firstName: String,
lastName: String,
address: Address)
case class Address(id: Option[Long],
name: String,
number: Int)
val personOrAddress:AnyRef= Person(Some(1L), "first", "last", Address(Some(1L), "street", 1))
type HasCopyMethodWithId = _
val newId = Some(123L)
personOrAddress.asInstanceOf[HasCopyMethodWithId].copy(id = newId)
}
我怎样才能实现'type HasCopyMethodWithId',以便这段代码在运行时编译并且不会失败?
我试过了:
type HasCopyMethodWithId = {def copy(id: Option[Long]): AnyRef}
【问题讨论】:
-
我认为你不能,这些复制方法不同。您必须创建另一个方法,例如
copyId来使用复制。 -
可以通过宏检测到实际类型(不是一般结构类型)吗?