require “Actor with Channels” by using self-typing

This commit is contained in:
Roland 2013-01-31 08:49:12 +01:00
parent 6e6e7f7e55
commit aec29618e3
4 changed files with 37 additions and 33 deletions

View file

@ -11,6 +11,7 @@ import scala.reflect.runtime.universe._
import akka.actor.Props
import scala.reflect.ClassTag
import scala.reflect.runtime.universe
import akka.actor.Actor
object ChannelExt extends ExtensionKey[ChannelExtension]
@ -19,6 +20,6 @@ class ChannelExtension(system: ExtendedActorSystem) extends Extension {
// kick-start the universe (needed due to thread safety issues in runtime mirror)
private val t = typeTag[(Int, Int) :+: TNil]
def actorOf[Ch <: ChannelList](factory: Channels[TNil, Ch]): ChannelRef[Ch] =
def actorOf[Ch <: ChannelList](factory: Actor with Channels[TNil, Ch]): ChannelRef[Ch] =
new ChannelRef[Ch](system.actorOf(Props(factory)))
}

View file

@ -40,7 +40,7 @@ import akka.actor.ActorInitializationException
* erased type, which may be less precise than the actual channel type; this
* can lead to ClassCastExceptions if sending through the untyped ActorRef
*/
trait Channels[P <: ChannelList, C <: ChannelList] extends Actor {
trait Channels[P <: ChannelList, C <: ChannelList] { this: Actor
import macros.Helpers._
@ -49,7 +49,7 @@ trait Channels[P <: ChannelList, C <: ChannelList] extends Actor {
* actor can handle everything which the child tries to send via its
* `parent` ChannelRef.
*/
def createChild[Pa <: ChannelList, Ch <: ChannelList](factory: Channels[Pa, Ch]): ChannelRef[Ch] = macro macros.CreateChild.impl[C, Pa, Ch]
def createChild[Pa <: ChannelList, Ch <: ChannelList](factory: Actor with Channels[Pa, Ch]): ChannelRef[Ch] = macro macros.CreateChild.impl[C, Pa, Ch]
/**
* Properly typed ChannelRef for the context.parent.

View file

@ -6,14 +6,15 @@ import ru.TypeTag
import scala.reflect.macros.Context
import scala.reflect.api.Universe
import akka.actor.Props
import akka.actor.Actor
object CreateChild {
import Helpers._
def impl[MyChannels <: ChannelList: c.WeakTypeTag, ParentChannels <: ChannelList: c.WeakTypeTag, ChildChannels <: ChannelList: c.WeakTypeTag](
c: Context {
type PrefixType = Channels[_, MyChannels]
})(factory: c.Expr[Channels[ParentChannels, ChildChannels]]): c.Expr[ChannelRef[ChildChannels]] = {
type PrefixType = Actor with Channels[_, MyChannels]
})(factory: c.Expr[Actor with Channels[ParentChannels, ChildChannels]]): c.Expr[ChannelRef[ChildChannels]] = {
import c.universe._