Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Self reference types
trait DB { def startDB: Unit } // defines an abstract start for a DB component
trait MT { def startMT: Unit } // defines an abstract start for an MT component
trait Oracle extends DB { def startDB = println("Starting Oracle") } // Some actual concrete instances.. dummied for example
trait Service extends MT { def startMT = println("Starting Service") }
trait App {
self: DB with MT => // declare that self for App refers to a DB with MT, so that we have access to the startXX ops
startDB
startMT
}
object DummyApp extends App with Oracle with Service // create a concrete instance with an actual DB and MT instance
DummyApp.run // run it and see "Starting Oracle" then "Starting Service"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.