0
我有一個函數,它接受一個Writes[Class.type]
作爲參數,當我通過它,它說,實際傳遞的參數是Writes[Class]
並拒絕編譯。`Writes [MyClass]`和`Writes [MyClass.type]`之間的區別?
兩者有什麼區別?
我有一個函數,它接受一個Writes[Class.type]
作爲參數,當我通過它,它說,實際傳遞的參數是Writes[Class]
並拒絕編譯。`Writes [MyClass]`和`Writes [MyClass.type]`之間的區別?
兩者有什麼區別?
Class
指的是一個名爲Class
類型。 Class.type
是指名爲Class
的對象的類型。
考慮以下代碼爲例:
class Foo {
val x = 42
}
object Foo {
val y = 23
}
def f(foo: Foo) {
println(foo.x)
// The next line wouldn't work because the Foo class does not have a member y:
// println(foo.y)
}
def g(foo: Foo.type) {
println(foo.y)
// The next line wouldn't work because the Foo object does not have a member x:
println(foo.x)
}
val foo1 = new Foo
val foo2 = new Foo
f(foo1)
f(foo2)
// Does not work because the object Foo is not an instance of the class Foo:
// f(Foo)
g(Foo)
// Does not work because g only accepts the object Foo:
// g(foo1)
謝謝,這是我失蹤了。 – gurghet