我试图创建一个与依赖类型相关的类型别名_ >: a.type
。
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
为什么?限制的目的是什么?