I have this question based on the Akka Concurrency pre-print (the final version isn't available yet), where this is used without explanation.
In version 1 of the class, they have:
class Altimeter extends Actor with ActorLogging with EventSource{
...
}
The claim is that Altimeter is now closely couple with EventSource, so testing Altimeter requires also testing EventSource. The suggested change to fix that is as follows:
--- a/src/main/scala/Altimeter.scala
+++ b/src/main/scala/Altimeter.scala
@@ -9,9 +9,10 @@ import scala.concurrent.ExecutionContext.Implicits.global
object Altimeter{
case class RateChange(amount: Float) //sent to Altimeter
case class AltitudeUpdate(altitude: Double)
+ def apply() = new Altimeter with ProductionEventSource
}
-class Altimeter extends Actor with ActorLogging with EventSource{
+class Altimeter extends Actor with ActorLogging{ this: EventSource =>
import Altimeter._
val ceiling = 43000 //feet
diff --git a/src/main/scala/EventSource.scala b/src/main/scala/EventSource.scala
index 1fd5578..ded4f38 100755
--- a/src/main/scala/EventSource.scala
+++ b/src/main/scala/EventSource.scala
@@ -8,7 +8,12 @@ object EventSource{
case class UnregisterListener(listener: ActorRef)
}
-trait EventSource { this: Actor =>
+trait EventSource{
+ def sendEvent[T](event: T): Unit
+ def eventSourceReceive: Actor.Receive
+}
+
+trait ProductionEventSource { this: Actor =>
import EventSource._
My first problem is that I don't understand what this: SomeTraitType =>
accomplishes. Is it literally saying that the class is of type SomeTraitType?
My second question is, what's the difference between that and using extends SomeTraitType
, and why is it advantageous? Perhaps that would be obvious if I knew it the former meant.