Skip to content

Instantly share code, notes, and snippets.

View sullivan-'s full-sized avatar

John Sullivan sullivan-

View GitHub Profile
trait TopLevelComponent extends
BlogRepoComponent with
PostRepoComponent with
AuthRepoComponent with
BlogServComponent with
PostServComponent with
AuthServComponent
abstract class Application extends TopLevelComponent {
// can access blogRepo, postRepo, authRepo, blogServ, postServ,
// and authServ here
}
trait TopLevelComponentImpl extends TopLevelComponent with
BlogRepoComponentImpl with
PostRepoComponentImpl with
AuthRepoComponentImpl with
BlogServComponentImpl with
PostServComponentImpl with
AuthServComponentImpl
new Application with TopLevelComponentImpl
trait AuthRepo {
def create(auth: Author): AuthId
def retrieve(id: AuthId): Author
def update(auth: Author): Unit
def delete(auth: Author): Unit
}
class AuthRepoImpl extends AuthRepo {
def create(auth: Author) = println(s"create $auth")
def retrieve(id: AuthId) = {
trait AuthRepoComponent {
val authRepo: AuthRepo
}
trait AuthRepoComponentImpl extends AuthRepoComponent {
val authRepo: AuthRepo = new AuthRepoImpl
}
trait AuthServComponent {
val authServ: AuthServ
trait TopLevelComponent {
val blogRepo: BlogRepo
val postRepo: PostRepo
val authRepo: AuthRepo
val blogServ: BlogServ
val postServ: PostServ
val authServ: AuthServ
}
trait TopLevelComponentImpl extends TopLevelComponent {
trait AuthServComponent {
val authServ: AuthServ
}
trait AuthServComponentImpl extends AuthServComponent {
self: AuthRepoComponent => // this is the self-type
val authServ: AuthServ = new AuthServ(authRepo)
}
trait AServiceComponent {
self: BServiceComponent =>
val aService = new AService
class AService {
println(s"hi from $this constructor, b is $bService")
def greet: Unit =
println(s"hi from $this greet, b is $bService")
}
}
trait TopComponent extends AServiceComponent with BServiceComponent
object Application extends App with TopComponent {
aService.greet
bService.greet
}