Reputation: 21220
I was reading up on best practices for implementing Slick, and was examining this example. In it, there is this construct:
trait BankRepository extends BankTable { this: DBComponent =>
... //A bunch of code
}
I don't understand the this: DBComponent =>
part. In this case, DBComponent
is a simple trait defined elsewhere (you can find it in the above link). What I don't understand is:
this: DBComponent =>
construct do. My IDE doesn't complain, but it also doesn't link to the function being executed by the =>
. My intuition is that it's saying the rest of the code is a value that is returned, but I'm not clear on what is invoking it, or what the value returned exactly.Upvotes: 1
Views: 34
Reputation: 51271
It's called a self type. It's basically a contract that says any class extending this trait (mixing it in) must include DBComponent
. And, as such, the compiler should assume DBCompenent
elements are in scope for the following code.
Here's a link to a description of it from Programming in Scala, Odersky et al, 1st Edition (a little dated but still accurate on most topics).
Upvotes: 4