Skip to content

Instantly share code, notes, and snippets.

@chrislewis
Created February 15, 2011 01:27
Show Gist options
  • Save chrislewis/826931 to your computer and use it in GitHub Desktop.
Save chrislewis/826931 to your computer and use it in GitHub Desktop.
/* Quick sketch after reading http://lssn.me/DtP.
Usage: File("a_file") << "some text"
Scala needs not the compiler's blessing for sexy. */
import java.io.{FileWriter, Writer}
/* A loaner for writers. */
trait WriterLoaner {
def write(w: Writer)(op: Writer => Unit) {
try {
op(w)
} finally {
w.close()
}
}
}
class WriteNow(val name: String) extends WriterLoaner {
/* writer per invocation */
def writer = new FileWriter(name)
/* cout anyone? */
def <<(input: String) {
write(writer) {
_.write(input)
}
}
}
object File {
def apply(name: String) = new WriteNow(name)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment