是什么声明字段作为之间的差异val,lazy val以及objectScala的类中,如下面的代码片断:
class A
class B {
val a1 = new A { def foo = 1 }
object a2 extends A { def foo = 1 }
lazy val a3 = new A { def foo = 1 }
}
Run Code Online (Sandbox Code Playgroud) 请参阅下面的源代码.所有源代码都在同一个包中定义.当我定义一个源文件中的所有代码ShowMain.scala,我得到一个编译错误,但是当object ShowMain在被定义ShowMain.scala并trait Show与object Show中定义Show.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]"
}
}
Run Code Online (Sandbox Code Playgroud)
编译错误:
(ScalaIDE/Scala 2.11.2在线包含output("hello"))
Multiple markers at this line
- not enough arguments for method output: …Run Code Online (Sandbox Code Playgroud)