Skip to content

Instantly share code, notes, and snippets.

@edrex-janrain
Last active December 19, 2015 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edrex-janrain/5946323 to your computer and use it in GitHub Desktop.
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
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