給定的情況下類:返回<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