给定的情况下类:返回<Case Class> .TYPE与案例
scala> case class Foo(x: Int, y: String)
defined class Foo
我可以定义返回Either[Foo.type, ...]
的方法。
scala> def f: Either[Foo.type, Int] = Left(Foo)
f: Either[Foo.type,Int]
当我试图去构建Foo
,我看到了一个编译时错误:
scala> f match { case Left(Foo(a, b)) => a }
<console>:14: error: constructor cannot be instantiated to expected type;
found : Foo
required: Foo.type
f match { case Left(Foo(a, b)) => a }
但以下工作:
scala> f match { case Left(foo) => foo }
<console>:14: warning: match may not be exhaustive.
It would fail on the following input: Right(_)
f match { case Left(foo) => foo }
res1: Foo.type = Foo
给定一个case class
,当它适合使用<CASE CLASS>.type
类型?
大概几乎从来没有? 'Foo.type'是伴侣对象的类型。 –
也许你想要'[Foo,Int]'而不是'[Foo.type,Int]'。 – Jesper