The easiest way to do something like this is to extend Traversable
. You only have to define foreach
, and you have full control over the traversal, so you can do things like open and close the file.
You can also extend Iterable
, which requires defining iterator
and, of course, returning some sort of Iterator
. In this case, you'd probably create an Iterator
for the disk data, but it's going to be much harder to control things like open files.
Here's one example of a Traversable
such as I described, written by Josh Suereth:
class FileLinesTraversable(file: java.io.File) extends Traversable[String] {
override def foreach[U](f: String => U): Unit = {
val in = new java.io.BufferedReader(new java.io.FileReader(file))
try {
def loop(): Unit = in.readLine match {
case null => ()
case line => f(line); loop()
}
loop()
} finally {
in.close()
}
}
}