How to write to a file in Scala?

This is one of the features missing from standard Scala that I have found so useful that I add it to my personal library. (You probably should have a personal library, too.) The code goes like so: def printToFile(f: java.io.File)(op: java.io.PrintWriter => Unit) { val p = new java.io.PrintWriter(f) try { op(p) } finally { … Read more

What do

These are called generalized type constraints. They allow you, from within a type-parameterized class or trait, to further constrain one of its type parameters. Here’s an example: case class Foo[A](a:A) { // ‘A’ can be substituted with any type // getStringLength can only be used if this is a Foo[String] def getStringLength(implicit evidence: A =:= … Read more

Scala 2.8 breakOut

The answer is found on the definition of map: def map[B, That](f : (A) => B)(implicit bf : CanBuildFrom[Repr, B, That]) : That Note that it has two parameters. The first is your function and the second is an implicit. If you do not provide that implicit, Scala will choose the most specific one available. … Read more