Why can't Scala find my typeclass instance defined implicitly in the companion object, when the typeclass is not in a dedicated source file?

不打扰是莪最后的温柔 提交于 2019-12-17 21:12:51

问题


Please refer to the source code below. All source code is defined in the same package. When I define all of the code within a single source file ShowMain.scala, I get a compile error, however when object ShowMain is defined in ShowMain.scala and trait Show and object Show are defined in Show.scala, there is no compile error.

My question: What is the cause for this? What language rule have I run afoul of?

Example code:

object ShowMain {

  def main(args: Array[String]): Unit = {
    output("hello")
  }

  def output[A](a: A)(implicit show: Show[A]) =
    println(show.show(a))

}

trait Show[-A] {
  def show(a: A): String
}

object Show {

  implicit object StringShow extends Show[String] {
    def show(s: String) = s"[String: $s]"
  }

}

Compile error:

(ScalaIDE/Scala 2.11.2 on line containing output("hello"))

Multiple markers at this line
    - not enough arguments for method output: (implicit show: test.typeclasses.show1.Show[String])Unit. Unspecified value 
     parameter show.
    - not enough arguments for method output: (implicit show: test.typeclasses.show1.Show[String])Unit. Unspecified value 
     parameter show.
    - could not find implicit value for parameter show: test.typeclasses.show1.Show[String]
    - could not find implicit value for parameter show: test.typeclasses.show1.Show[String]

回答1:


There's a rule that implicit has to be defined earlier in the compilation unit.

So, move object Show to the top and it compiles.

Alternatively,

object Show {
  //implicit object StringShow extends Show[String] {
  implicit val x: Show[String] = new Show[String] {
    def show(s: String) = s"[String: $s]"
  }
}

see the explanation about the type:

https://stackoverflow.com/a/2731285/1296806



来源:https://stackoverflow.com/questions/25468583/why-cant-scala-find-my-typeclass-instance-defined-implicitly-in-the-companion-o

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