1
我試圖創建與依賴類型_ >: a.type
相關的類型別名。爲什麼Scala編譯器禁止將通配符類型聲明爲類型參數的超類型
Scala編譯器報告,我不明白的錯誤:
scala> def foo[A](a: A) = {
| type F = Function1[_ >: a.type, Unit]
| }
<console>:12: error: type mismatch;
found : a.type (with underlying type A)
required: AnyRef
Note that A is unbounded, which means AnyRef is not a known parent.
Such types can participate in value classes, but instances
cannot appear in singleton types or in reference comparisons.
type F = Function1[_ >: a.type, Unit]
^
如果我更換a: A
到a: A with AnyRef
,它的工作原理:
scala> def foo[A](a: A with AnyRef) = {
| type F = Function1[_ >: a.type, Unit]
| }
foo: [A](a: A with AnyRef)Unit
爲什麼? 限制的目的是什麼?
我還是不明白它爲什麼會影響_>:a.type。我認爲像_>:a.type這樣的存在類型永遠不會涉及類型相等檢查。 –
我假設應用保護措施時不考慮上下文;讓我們希望斯卡拉語言大師之一將澄清。 – devkat