Skip to content

Instantly share code, notes, and snippets.

@gvolpe
Last active December 20, 2017 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gvolpe/adc0faec0ce129a675afef7977807a65 to your computer and use it in GitHub Desktop.
Save gvolpe/adc0faec0ce129a675afef7977807a65 to your computer and use it in GitHub Desktop.
Simple Typed Actor
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