Last active
August 29, 2015 14:21
-
-
Save lambdista/04a11dcd2955cefdd0ff to your computer and use it in GitHub Desktop.
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
object Poly { | |
trait Case[A] { | |
type Result | |
def at(x: A): Result | |
} | |
trait PolyDef { | |
def apply[A : Case](x: A): Case[A]#Result | |
} | |
object Length extends PolyDef { | |
override def apply[A : Case](x: A): Case[A]#Result = implicitly[Case[A]].at(x) | |
} | |
implicit object IntCase extends Case[Int] { | |
override type Result = Int | |
override def at(x: Int) = 4 | |
} | |
implicit object StringCase extends Case[String] { | |
override type Result = Int | |
override def at(x: String) = x.length | |
} | |
def main(args: Array[String]): Unit = { | |
println(Length(1)) | |
println(Length("hello")) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment