Skip to content

Instantly share code, notes, and snippets.

@frgomes
Last active May 25, 2017 22:22
Show Gist options
  • Select an option

  • Save frgomes/8e3262a346b90b3ab3d3 to your computer and use it in GitHub Desktop.

Select an option

Save frgomes/8e3262a346b90b3ab3d3 to your computer and use it in GitHub Desktop.
Scala - Early initialization block after new
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() }
}
val acct = new { // Early definition block after new
val filename = "myapp.log"
} with FileLogger
acct.info("hey!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment