Created
November 1, 2013 10:22
-
-
Save mzimecki/7263509 to your computer and use it in GitHub Desktop.
[Scala] Views example from Scala in Depth book
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
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