我在文件中有以下定义Show.scala:
package com.example
trait Show[A] {
def show(a: A): String
}
object Show {
def apply[A](implicit sh: Show[A]): Show[A] = sh
//def show[A](a:A)(implicit sh: Show[A]) = sh.show(a)
def show[A: Show](a: A) = Show[A].show(a)
implicit class ShowOps[A: Show](a: A) {
def show = Show[A].show(a)
}
implicit val intCanShow: Show[Int] =
new Show[Int] {
override def show(a: Int): String = s"int $a"
}
}
并在Main.scala:
package com.example
object Main extends App {
println(Show.show(344))
println(30.show)
}
编译器抱怨:
[error] /home/developer/scala/show/src/main/scala/com/example/Main.scala:6:14: value show is not a member of Int
[error] println(30.show)
[error] ^
[error] one error found
我究竟做错了什么?