的签名Predef.classOf https://github.com/scala/scala/blob/bda011524f7619b61b4fcc8172f088d2445e947e/src/library/scala/Predef.scala#L122 is
def classOf[T]: Class[T]
所以模拟上面的签名
object Foo
def f[T]: Class[T] = null
为什么下面的编译
f[Foo.type] // ok
但Scala中出现以下错误
classOf[Foo.type] // error: class type required but Foo.type found
编辑:在 Scala 2.13.4 及更高版本中,上述内容确实有效(如评论中指出的),但是在以下场景中仍然会发生类似的编译时错误
val foo: Foo.type = Foo
f[foo.type] // ok
classOf[foo.type] // error: class type required but foo.type found
None
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)