I have a question regarding the following code:
trait Connection {
def query(q: String): String
}
trait Logger {
def log(l: String): Unit
}
trait RequiredServices {
def makeDatabaseConnection: Connection
def logger: Logger
}
trait TestServices extends RequiredServices {
def makeDatabaseConnection = new Connection { def query(q: String) = "test" }
def logger = new Logger { def log(l: String) = println(l) }
}
trait ProductFinder { this: RequiredServices =>
def findProduct(productId: String) = {
val c = makeDatabaseConnection
c.query(productId)
logger.log("querying database..")
}
}
object FinderSystem extends ProductFinder with TestServices
My question especially relates to the following portion of code:
this: RequiredServices =>
def findProduct(productId: String) = {
...
What is the name of the Scala construct of the above? i.e. the this: RequiredServices =>
Thanks in advance for your replies.
The name is self type annotation. It is explained in §5.1 (Templates) of the Scala Language Specification, as follows: