Created
February 15, 2011 01:27
-
-
Save chrislewis/826931 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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