`Writes [MyClass]`和`Writes [MyClass.type]`之间的区别?

时间:2017-07-19 14:59:12

标签: json scala types playframework

我有一个以.box { margin-bottom: 0; } [class*="col-lg-"],[class*="col-md-"], [class*="col-sm-"],[class*="col-xs-"]{ padding-right:0 !important; padding-left:0 !important; } 为参数的函数,当我传递它时,它表示实际传递的参数是Writes[Class.type]并且拒绝编译。

两者有什么区别?

1 个答案:

答案 0 :(得分:4)

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)