Skip to content

Instantly share code, notes, and snippets.

@mzimecki
Created November 1, 2013 10:22
Show Gist options
  • Save mzimecki/7263509 to your computer and use it in GitHub Desktop.
Save mzimecki/7263509 to your computer and use it in GitHub Desktop.
[Scala] Views example from Scala in Depth book
import FileWrapper.wrap
/**
* Views example: converting one type to other.
*/
class FileWrapper(val file: java.io.File) {
def /(next: String) = new FileWrapper(new java.io.File(file, next))
override def toString = file.getCanonicalPath()
}
object FileWrapper {
implicit def wrap(file: java.io.File) = new FileWrapper(file)
implicit def unwrap(wrapper: FileWrapper) = wrapper.file
}
object MainFileWrapper {
def main(args: Array[String]) {
val cur = new java.io.File(".")
val tempFile = cur / "temp.txt" //tempFile will be implicitly converted to FileWrapper
useFile(tempFile) //tempFile will be implicitly converted to File
}
def useFile(file: java.io.File) = file.getCanonicalPath()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment