1

以下のソースコードを参照してください。すべてのソース コードは同じパッケージで定義されます。1 つのソース ファイル内ですべてのコードを定義するとShowMain.scalaコンパイル エラーが発生しますが、 およびが で定義されているobject ShowMain場合、コンパイル エラーは発生しません。ShowMain.scalatrait Showobject ShowShow.scala

私の質問: これの原因は何ですか? 私が違反した言語規則は何ですか?

コード例:

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]"
  }

}

コンパイル エラー:

(を含む行の ScalaIDE/Scala 2.11.2 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]
4

1 に答える 1