Skip to content

Instantly share code, notes, and snippets.

@lambdista
Last active August 29, 2015 14:21
Show Gist options
  • Save lambdista/04a11dcd2955cefdd0ff to your computer and use it in GitHub Desktop.
Save lambdista/04a11dcd2955cefdd0ff to your computer and use it in GitHub Desktop.
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