Created
November 7, 2014 11:55
-
-
Save noelwelsh/1da9f6bf2f80d17ee938 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
// 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