I have some troubles having Scala to infer the right type from a type projection.
Consider the following:
trait Foo {
type X
}
trait Bar extends Foo {
type X = String
}
def baz[F <: Foo](x: F#X): Unit = ???
Then the following compiles fine:
val x: Foo#X = ???
baz(x)
But the following won't compile:
val x: Bar#X = ???
baz(x)
Scala sees the "underlying type String" for x
, but has lost the information that x
is a Bar#X
. It works fine if I annotate the type:
baz[Bar](x)
Is there a way to make Scala infer the right type parameter for baz
?
If not, what is the general answer that makes it impossible?
x
using a type designator instead of a type projection, it works—including e.g.object BAR extends Bar; val x: BAR.X = "a"; baz(x)
. – Travis Brownx
to be typed as something more or less likeBar#X
with the incredibly uglyval x: b.X forSome { val b: Bar } = "a": b.X forSome { val b: Bar }
. – Travis BrownRDF
trait directly, then I can't benefit from many of the implicit functions like this one. – betehess