Last active
December 20, 2017 07:37
-
-
Save gvolpe/adc0faec0ce129a675afef7977807a65 to your computer and use it in GitHub Desktop.
Simple Typed Actor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import akka.actor.Actor | |
/** | |
* Simple [[TypedActor]] that gives any class implementing it the power to have typed messages making | |
* proper use of the compiler for type check exhaustiveness by just using a typed [[Function1]]. | |
* | |
* For convenience use this trait instead of using directly [[Actor]] unless you have a good reason. | |
* */ | |
trait TypedActor[A] extends Actor { | |
type TypedReceive = A => Unit | |
def typedReceive: TypedReceive | |
private def liftToPF[X <: Y, W, Y](f: Function[X, W])(implicit ct: ClassTag[X]): PartialFunction[Y, W] = | |
new PartialFunction[Y, W] { | |
override def isDefinedAt(x: Y): Boolean = ct.runtimeClass.isInstance(x) | |
override def apply(v1: Y): W = f(v1.asInstanceOf[X]) | |
} | |
override def receive: Receive = liftToPF(typedReceive) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment