I am busy with a major refactoring task on some legacy Scala/Akka code, and am being haunted by a construct that I am unable to explain, which is hampering my effort:
trait PerRequestCreator {
this: Actor =>
def perRequest(<some_params>): ActorRef = { body of function }
}
Which is then used as such:
class SomeActor extends PerRequestCreator with Actor {
def processRequest: Route = {
perRequest(<some_params_passed>)
}
}
I am having trouble understanding the this: Actor => ...
part of the trait.
It's called self-type, and it expresses a requirement for the
PerRequestCreator
to be mixed in into something that extendsActor
.It's useful because now you can use anything defined in
Actor
inside the definition ofPerRequestCreator
and the compiler will check that you can only extendPerRequestCreator
if you also extendActor
.Example:
You can read more about it here: https://docs.scala-lang.org/tour/self-types.html