I always believed that type aliases always expand to their original type if necessary. But, here is a troublemaker
def a[P](a: Option[P]) = {
type Res = List[P] // result type alias
Nil: Res // Replace this line with Nil: List[P] to clear the error
}
def b[V](v: V) = a(Some(v)): List[V]
it fails with (scastie)
error: type mismatch;
found : Res (which expands to) List[P]
required: List[V]
You see that a
converts Option[P] => List[P]
and, since b
supplies Some[V]
, a
converts Option[V] => List[V]
when b
calls it. But, compiler says that result is incompatible with List[V]
. How is this possible? The error goes away (scastie) if you replace Nil: Res
with Nil: List[P]
in the a
. You need to eliminate the type alias to get rid of the error. This means that the type alias is the culprit.