Created
June 16, 2011 21:38
-
-
Save kings13y/1030348 to your computer and use it in GitHub Desktop.
Generalized type constraints example
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
case class PrintFormatter[T](item : T) { | |
def formatString(implicit evidence: T =:= String) = { // Will only work for String PrintFormatters | |
println("STRING specialised printformatting...") | |
} | |
def formatPrimitive(implicit evidence: T <:< AnyVal) = { // Will only work for Primitive PrintFormatters | |
println("WRAPPED PRIMITIVE specialised printformatting...") | |
} | |
} | |
val stringPrintFormatter = PrintFormatter("String to format...") | |
stringPrintFormatter formatString | |
// stringPrintFormatter formatPrimitive // Will not compile due to type mismatch | |
val intPrintFormatter = PrintFormatter(123) | |
intPrintFormatter formatPrimitive | |
// intPrintFormatter formatString // Will not compile due to type mismatch |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment