问题
What is the supertype for all Scalaquery queries?
As far as i have understood, Query[Projection[Product]]
should be it, e.g.:
Projection2[Int, Int]
<: Projection[Tuple2[Int,Int]]
<: Projection[Product]
so val query: Query[Projection[Product]] = for (all <- Tab) yield all.*
should work for Tab = new Table[(Int, Int)] {…}
…but appearantly i don’t understand how typing in scala works.
I’m totally confused, so if i missed something, please ask.
回答1:
This doesn't work because the type parameter for Projection is invariant and it would need to be covariant for Projection[Product]
to be a supertype of Projection[(Int,Int)]
. Thus Query[Projection[Product]]
is not a supertype of Query[Projection[(Int,Int)]]
, which is the reason why the compiler is complaining.
Everything clear? If not, read about invariance and covariance in wikipedia and in the Scala reference.
The type of all Querys of Projections of X, where X is a subtype of Product, is Query[Projection[X]] forSome { type X <: Product }
.
来源:https://stackoverflow.com/questions/6126076/supertype-for-scalaquery-query