Last active
December 19, 2015 11:18
-
-
Save edrex-janrain/5946323 to your computer and use it in GitHub Desktop.
Trying to understand why mapping over a function with a context bound requires setting the type explicitly
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
abstract class Ponies[T] { | |
def ponies(a: T) : String | |
} | |
def bar[T](obj: T)(implicit p : Ponies[T]) = p.ponies(obj) | |
def baz[T](obj: T) = obj | |
def qux(obj: Int)(implicit p : Ponies[Int]) = p.ponies(obj) | |
def fred[T: Ponies(obj: T) = implicitly[Ponies[T]].ponies(obj) | |
def foo { | |
implicit object IntCanHazPonies extends Ponies[Int] { | |
def ponies(a: Int) = s"$a can haz ponies!" | |
} | |
// These fail with | |
// "could not find implicit value for parameter p: this.Ponies[T]" | |
// Some(2).map(bar) | |
// Some(6).map(fred) | |
// These all work | |
bar(1) | |
Some(3).map(bar[Int]) | |
Some(4).map(baz) | |
Some(5).map(qux) | |
} | |
foo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment