Skip to content

Instantly share code, notes, and snippets.

@binshuohu
Created May 6, 2016 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save binshuohu/c331673845ebf7cec7a3b28bee900271 to your computer and use it in GitHub Desktop.
Save binshuohu/c331673845ebf7cec7a3b28bee900271 to your computer and use it in GitHub Desktop.
how to modularize scala code properly?
trait FooComponent {
def fooService: FooService
trait FooService {
def query: String
}
}
trait ConcreteFooComponent extends FooComponent {
val fooService = ???
}
trait QueryRoutes {
this: FooComponent =>
val queryRoute = get {
complete(fooService.query)
}
}
trait OtherRoutes {
val otherRoute = ???
}
trait HttpService extends QueryRoutes with OtherRouters { // 编译不过,因为QueryRoutes声明了self type annotation
val route = queryRoute ~ otherRoute
}
object Main extends HttpService with ConcreteFooComponent with App {
}
@pxbin
Copy link

pxbin commented May 6, 2016

trait HttpService extends QueryRoutes with OtherRouters {
this: FooComponent =>
val route = queryRoute ~ otherRoute
}

@1178615156
Copy link

trait HttpService extends QueryRoutes with OtherRouters {
//加上
this: FooComponent =>
val route = queryRoute ~ otherRoute
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment