Nathaniel Ford
Nathaniel Ford

Reputation: 21220

this: SomeObject => object construct

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:

Upvotes: 1

Views: 34

Answers (1)

jwvh
jwvh

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

Related Questions