1

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.

flying sheep
  • 8,475
  • 5
  • 56
  • 73
  • If you don't describe what isn't working, what error messages are appearing, what the result is and what was the result you expected, etc, you are unlikely to get good help. – Daniel C. Sobral May 25 '11 at 18:04
  • I actually found the question quite understandable, even though I am unfamiliar with scalaquery. – Kim Stebel May 25 '11 at 19:14

1 Answers1

2

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 }.

Kim Stebel
  • 41,826
  • 12
  • 125
  • 142
  • and what would be a supertype of all queries? – flying sheep May 26 '11 at 08:55
  • Query[Any], because Query's type parameter is covariant. If it was contravariant, it would be Query[Nothing] and if it was invariant, there wouldn't be a supertype of all Query[X]. There is also the existential type Query[_]. Your problem, however, is that Projection is invariant. – Kim Stebel May 26 '11 at 08:57
  • so there is no way to specify a type that allows to store all kinds of queries into a variable with that type, and still access query results like the products that they are, without resorting to casting? – flying sheep May 26 '11 at 09:15
  • You can probably do that with existential types. Something like `Query[Projection[X]] forSome { type X <: Product }` – Kim Stebel May 26 '11 at 09:46
  • yay, that was it! Now i have the shorthand `Query[Projection[_ <: Product]]`. thanks for showing me that concept, you should edit it into your answer :) – flying sheep May 26 '11 at 10:16