It is not clear from your question exactly how you want to use such a thing. An example of what you are trying to do may help.
Here's an implicit function that will add a simple statement that writes any object as a string to a file. (Note that I use >>
to indicate a unix style >
, since >
already has a value in Scala ("less"). You can replace it with some other statement if you want.)
implicit def anyToFileOutput(self: Any) = new { import java.io._ def >>(filename: String) { val f = new BufferedWriter(new FileWriter(filename)) try { f.write(self.toString) } finally { if (f != null) f.close() } } }
You would use it as follows:
scala> List(1,2,3) >> "out.txt"
What creates the file "out.txt" in the working directory containing List(1, 2, 3)
source share