Skip to content

Instantly share code, notes, and snippets.

@frgomes
Last active May 25, 2017 22:22
Show Gist options
  • Save frgomes/f580bae078d4c83993ef to your computer and use it in GitHub Desktop.
Save frgomes/f580bae078d4c83993ef to your computer and use it in GitHub Desktop.
Scala - Early initialization block after extends
trait Logger {
def log(msg: String)
def info(msg: String) { log("INFO: " + msg) }
def warn(msg: String) { log("WARN: " + msg) }
def severe(msg: String) { log("SEVERE: " + msg) }
}
trait FileLogger extends Logger {
val filename: String
val out = new java.io.PrintWriter(filename)
out.println("# " + new java.util.Date().toString)
def log(msg: String) { out.println(msg); out.flush() }
}
class SavingsAccount extends { // Early definition block after extends
val filename = "savings.log"
} with FileLogger {
// SavingsAccount implementation
}
val acct = new SavingsAccount
acct.info("hey!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment