import scalaz._; import Scalaz._
def foo[M[_]:MonadPlus,A](a:A) = a.point[M]
// foo: [M[_], A](a: A)(implicit evidence$1: scalaz.MonadPlus[M])M[A]
def bar1[M[_]:MonadPlus](i:Int): M[Int] =
foo(i) // <-- error: ambiguous implicit values
// this works, but why? Isn't it just the same?
def bar2[M[_]:MonadPlus](i:Int): M[Int] =
foo(i)(implicitly[MonadPlus[M]])
def bar3[M[_]](i:Int)(implicit m:MonadPlus[M]): M[Int] =
foo(i)(m) // slightly less surprising that this works
def bar4[M[_]:MonadPlus](i:Int): M[Int] =
foo[M,Int](i) // this also works, but why?
build.sbt:
scalaVersion := "2.9.2"
libraryDependencies += "org.scalaz" %% "scalaz-core" % "7.0.0-M5"
(虽然我在2.10.0-RC3中得到了相同的结果)
最佳答案 如果您只是写下,则会收到相同的错误消息:
import scalaz._; import Scalaz._
def foo[M[_]:MonadPlus,A](a:A) = a.point[M]
foo(1) // <-- error: ambiguous implicit values
// both value listInstance ...
// and value optionInstance ...
我的理解是编译器试图对bar1方法的“主体”进行类型检查,而不考虑范围内可能存在MonadPlus [M]实例(由条形定义引入),它已经找到2个特定的MonadPlus [M]实例(listInstance和optionInstance)适用于foo调用.在这个阶段,它只是宣布一个模棱两可.
然后在bar2和bar3中明确指定要使用的实例,并在bar4中给出类型参数,这些参数是M和Int,在foo调用中不明确,因为具有这些约束的唯一隐含范围是隐式[MonadPlus [M]] .