Skip to content

Instantly share code, notes, and snippets.

@noelwelsh
Created November 7, 2014 11:55
Show Gist options
  • Save noelwelsh/1da9f6bf2f80d17ee938 to your computer and use it in GitHub Desktop.
Save noelwelsh/1da9f6bf2f80d17ee938 to your computer and use it in GitHub Desktop.
// Type class
trait Burp[A] {
def burp(in: A): String
}
object Burp {
def apply[A](implicit burp: Burp[A]): Burp[A] = burp
}
case class Foo(name: String)
object Foo {
// Type class instances
// Uncomment valOne and valTwo = ambiguity error
// Uncomment objectOne and objectTwo = ambiguity error
// Uncomment one val and one object = no ambiguity error
//
// Using Scala 2.10
implicit val valOne = new Burp[Foo] {
def burp(in: Foo): String = "valOne"
}
// //implicit val valTwo = new Burp[Foo] {
// def burp(in: Foo): String = "valTwo"
// }
implicit object objectOne extends Burp[Foo] {
def burp(in: Foo): String = "objectOne"
}
//implicit object objectTwo extends Burp[Foo] {
// def burp(in: Foo): String = "objectTwo"
//}
}
object Example {
def example = {
Burp[Foo].burp(Foo("hi"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment