Implicit not found when omitting empty argument list

故事扮演 提交于 2019-12-24 02:55:15

问题


I have the following (simplified) code:

case class Value[T](value: T)

trait Absable[In,Out] {
  def absoluteValue(in: In): Out
}

implicit class AbsValue[In, Out](in: Value[In]) {
  def abs()(implicit ev: Absable[In, Out]): Value[Out] = Value(ev.absoluteValue(in.value))
}

implicit def AbsNumeric[A : Numeric] = new Absable[A, A] { 
  def absoluteValue(in: A) = implicitly[Numeric[A]].abs(in) 
}

Now I want to use the abs function on a Value:

scala> Value(-3).abs()
res3: Value[Int] = Value(3)

scala> Value(-3).abs
<console>:14: error: could not find implicit value for parameter ev: Absable[Int,Nothing]
              Value(-3).abs
                        ^

I added an empty argument list in front of the implicit arguments to give callers more flexibility, but now when I omit the empty list at the call site the compiler can't find the implicit... So now instead of more flexibility callers get confusing compile errors.
I don't understand how leaving off the argument list can affect the type inference or implicit resolution.

I am using scala 2.11.6

来源:https://stackoverflow.com/questions/30863699/implicit-not-found-when-omitting-empty-argument-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!