0

Is it possible to match generic case classes? The idea is to use some generic case classes as messages between actors.

Thats my approach: Is there anyway to make this possible?

case class EbTreeDataObject[T](uId: Long, changeId: Long, payload:Option[T])
case class InsertNewObject[T](newObject: EbTreeDataObject[T])
case class UpdateObject[T](changedObject: EbTreeDataObject[T])


class TreeActor[T](maxSynchroWait:Int,communication:CommunikationLayer[T]) extends Actor {
  var uIdTree = new EbTree[EbTreeDataObject[T]]
  var changeIdTree = new EbTree[EbTreeDataObject[T]]

  override def receive: Receive = {
    //basic tree operations
    case InsertNewObject(newObject: EbTreeDataObject[T]) =>
       //insert element in tree
    case UpdateObject(newObject: EbTreeDataObject[T]) =>
       //update object
    case RemoveObject(removedObject: EbTreeDataObject[T]) =>
       //remove object

This constructs give an error while compiling:

Error:(32, 37) pattern type is incompatible with expected type;
 found   : model.EbTreeDataObject[T]
 required: model.EbTreeDataObject[Any]
Note: T <: Any, but class EbTreeDataObject is invariant in type T.
You may wish to define T as +T instead. (SLS 4.5)
    case InsertNewObject(newObject: EbTreeDataObject[T]) =>

Thanks for your help

prototyp
  • 570
  • 6
  • 20

1 Answers1

4

Why don't you do what the compiler suggests:

  case class EbTreeDataObject[+T](uId: Long, changeId: Long, payload: Option[T])
Noah
  • 13,821
  • 4
  • 36
  • 45
  • Thx but what is the difference between [+T] and [T]? – prototyp Jun 22 '14 at 22:05
  • 2
    `[T]` means only types of 'T' allowed, where '[+T]' means anything that's a subtype of `T`, `A <: T`. Here's a decent SO question that explains this in some more detail: http://stackoverflow.com/questions/663254/scala-covariance-contravariance-question – Noah Jun 22 '14 at 22:21