I use the cake template in Scala 2.10 to introduce the required trait to my actor according to some business logic:
I have several types of f Events:
sealed abstract class Event(val timeStamp:Long) case class StudentEvent(override val timeStamp:Long, studentId:Long) extends Event(timeStamp:Long) case class TeacherEvent(override val timeStamp:Long, teacherIdId:Long) extends Event(timeStamp:Long)
Now I have traits that implement an action for each type of event:
Annotation:
trait Action[T <: Event] { def act[T](event:T):Unit }
And two implants:
trait StudentAction extends Action[StudentEvent]{ override def act[StudentEvent](event:StudentEvent):Unit = println(event) }
and
trait TeacherAction extends Action[TeacherEvent]{ override def act[TeacherEvent](event:TeacherEvent):Unit = println(event) }
Now my actor:
class ActionActor[T <: Event] extends Actor{ self:Action[T]=> override def receive= { case msg: T => act(msg) case _ => println("Unknown Type") } }
And I enter the required trait as follows:
val actionActor = system.actorOf(Props(new ActionActor[StudentEvent] with StudentAction)) actionActor ! StudentEvent(1111L,222L)
When compiling, I get an error message:
Warning:(14, 14) abstract type pattern T is unchecked since it is eliminated by erasure case msg:T => act(msg) ^
I know that for some reason I need to use TypeTag, but I did not understand how to do this.
Please, help.
Update:
In fact, I have 10 types of events that extend from the event I need to handle.
I want to implement the business logic for each event in a separate attribute, because since mixing all 10 functions of the event handler will give me several hundred (if not thousands) lines of code.
I do not want to create different types of Actor for each event. For instance:
class Event1Actor extend Actor{ def receive ={ case Event1(e) =>
the same Event3Actor, Event4Actor, etc.
Such code seems ugly to me because I need to implement business logic within each Actor.
I am looking for some general solution based on a design pattern, for example, a strategy pattern.