简体   繁体   中英

Scala. Case class copy from trait reference

Hi I'm trying to solve a problem in kind of "elegant" and type safe way but I can't find the best...

Let's say I have this trait

trait Event {
  def deviceId: String
  def userId: String
  def eventDateTime: DateTime
  def payload: Option[Payload]
}

trait Payload

And following case classes (there could be more)

case class AEvent (deviceId: String, userId: String, eventDateTime: DateTime, payload: Option[APayload]) extends Event
case class APayload (content: String)


case class BEvent (deviceId: String, userId: String, eventDateTime: DateTime, payload: Option[BPayload]) extends Event
case class BPayload (size: Int, name: String)

I would like to use case class copy method directly from the trait without casting to AEvent or BEvent...

As I'm having a reference to the trait, best solution I figured out is to create a method like this:

def copy[T <: Event](event: T)(deviceId: String = event.deviceId,
                             userId: String = event.userId,
                             eventDateTime: DateTime = event.eventDateTime,
                             payload: Option[Payload] = event.payload) T = {
  val res = event match {
    case x: AEvent => AEvent(deviceId, userId, eventDateTime, payload.asInstanceOf[APayload])
    case x: BEvent => BEvent(deviceId, userId, eventDateTime, payload.asInstanceOf[BPayload])
  }
res.asInstanceOf[T]
}

What I don't like is that Payload type is casted in runtime... How can I have type check during compile time?

Thanks in advance

What about

case class Event[P <: Payload](deviceId: String, userId: String, eventDateTime: DateTime, payload: Option[P])

and using Event[APayload] instead of AEvent ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM