When you declare a generic event as an envelop containing some payload declared by a generic type parameter, the canPersist method of the Persister returns true for all Events even if the payload differs.
i.e.
case class Event[P](payload:P)
case class Payload1(msg:String)
case class Payload2(value:Int)
implicit val payload1Format = jsonFormat1(Payload1)
implicit val payload2Format = jsonFormat1(Payload2)
implicit val eventPayload1Format = jsonFormat(Event[Payload1])
implicit val eventPayload2Format = jsonFormat(Event[Payload2])
payload1Persister = persister[Event[Payload1]]("payload1")
payload2Persister = persister[Event[Payload2]]("payload2")
At compile time everything seams to be ok but at runtime the vm will raise a ClassCastException when you try to persist an event with payload2. This due to the fact that the Persister itself makes a type check of the Root element only:
private[stamina] def convertToT(any: AnyRef): Option[T] = any match {
case t: T ⇒ Some(t)
case _ ⇒ None
}
When you declare a generic event as an envelop containing some payload declared by a generic type parameter, the canPersist method of the Persister returns true for all Events even if the payload differs.
i.e.
At compile time everything seams to be ok but at runtime the vm will raise a ClassCastException when you try to persist an event with payload2. This due to the fact that the Persister itself makes a type check of the Root element only: