scala - Why is no implicit view found, when eta conversion and specifying type parameters does allow an implicit view to be found? -
the code:
object test { import scala.language.implicitconversions case class c1() {} case class c2() {} implicit def c1toc2(in: c1): c2 = c2() def from[a, b](in: a)(implicit f: => b): b = f(in) def fails(): future[c2] = { val future: future[c1] = future.successful(c1()) future.map(from) // line fails compile! } def compiles1(): future[c2] = { val future: future[c1] = future.successful(c1()) future.map(x => from(x)) } def compiles2(): future[c2] = { val future: future[c1] = future.successful(c1()) future.map(from[c1, c2]) } } in example, fails method fails compile. error message is:
error:(23, 16) no implicit view available => b. future.map(from)
i'm confused why no implicit view found. based on compiles1 , compiles2 methods, both compile, seems there is implicit view available => b.
what going on here, , why 2 compilesn methods work, fails not?
my background: i'm still learning scala, case i'm missing pretty obvious. :)
i'm on scala 2.11.8.
the compiler attempts resolve implicits before eta-expansion of from function, type parameters of from not yet inferred when call this:
future.map(from) compiles2 works because supply type parameters on own. when call future.map(from[c1, c2]), compiler knows need implicit c1 => c2, because that's you've told it.
with compiles1, difference little more subtle, stems fact future.map(from) , future.map(x => from(x)) different things. former uses eta-expansion, fails aforementioned reasons. future.map(x => from(x)), there no eta-expansion happening. instead, have anonymous function calls from instead of eta-expanding it. therefore, compiler can infer type of x, tells x c1 (in case), , can find implicit conversion c1toc2 satisfies type parameters of from while resolving implicit , final return type of method, future[c2].
Comments
Post a Comment