Existential types and type members
Asked Answered
F

2

12

What works (Part A)

Suppose I have a trait with a type parameter:

trait A[T]

I can use an existential type to write a method that will take a collection of As that all have the same T:

def foo(as: Seq[A[X]] forSome { type X }) = true

Note that this is different from the following:

def otherFoo(as: Seq[A[X] forSome { type X }]) = true

Or the equivalent:

def otherFoo(as: Seq[A[_]]) = true

In these cases the scope of the existential is inside the Seq, so the As can have different Ts. With my original foo (with the existential scoping over the Seq), the following is fine:

foo(Seq(new A[Int] {}, new A[Int] {}))

But make the type parameters different and it doesn't compile:

scala> foo(Seq(new A[Int] {}, new A[String] {}))
<console>:10: error: type mismatch;
 found   : Seq[A[_ >: java.lang.String with Int]]
 required: Seq[A[X]] forSome { type X }

              foo(Seq(new A[Int] {}, new A[String] {}))
                     ^

This is all pretty straightforward.

What works (Part B)

Now suppose I have a similar trait with a type member instead of a type parameter:

trait B { type T }

I can write a method that will only take a B with some specified T:

scala> def bar[X](b: B { type T = X }) = true
bar: [X](b: B{type T = X})Boolean

scala> bar[Int](new B { type T = Int })
res5: Boolean = true

scala> bar[String](new B { type T = Int })
<console>:10: error: type mismatch;
 found   : java.lang.Object with B
 required: B{type T = String}
              bar[String](new B { type T = Int })
                          ^

Again, this works exactly the way you'd expect it to.

What doesn't work

When we try to write the equivalent of our foo above, but for type members, things get weird.

scala> def baz(bs: Seq[B { type T = X }] forSome { type X }) = true
baz: (as: Seq[B{type T = X}] forSome { type X })Boolean

scala> baz(Seq(new B { type T = Int }, new B { type T = String }))
res7: Boolean = true

That the last line compiles makes no sense to me. I've told it that I want all the type members to be the same. My foo shows that I can do this for type parameters, and bar shows that I can constrain a type based on its type members. But I can't combine the two.

I've tried this on 2.9.2 and 2.10.0-M5.

Motivation

This question is inspired by this one, where my first thought was, oh, just use an existential type (setting aside for a second the issue that it seems to be impossible to get an existential type to scope of the type of a repeated parameter, which would be handy here):

def accept(rs: Seq[RList[Int] { type S = X }] forSome { type X }) = true

But this doesn't actually work—you get the same weird result as in the simplified example above.

Foresheet answered 21/8, 2012 at 1:31 Comment(0)
W
3

I've finally sort it out (at least I hope so). Let's do it the other way. We build our trait:

scala> trait B {type T}
defined trait B

We try and build a sequence of B:

scala> Seq(new B {type T = Int}, new B {type T = String})
res0: Seq[B{type T >: String with Int}] = List($anon$1@592b12d, $anon$2@61ae0436)

Damn, it works! Ok, we don't have an equality for type T but let's play with it:

scala> res0 : (Seq[B {type T = X}] forSome {type X >: String with Int})
res1: Seq[B{type T = X}] forSome { type X >: String with Int } = List($anon$1@592b12d, $anon$2@61ae0436)

It's closer. No wait, it's not closer, It's better than what you've proposed as a parameter of baz, we do not only provide a raw type, we also have a upper bound! Thus, we can clearly pass it to baz. This is why it doesn't work as you expected.

Warpath answered 21/8, 2012 at 13:31 Comment(2)
Thanks—I think this gets me closer to understanding what's going on here. I'm still not sure I grasp why this works but the type parameter equivalent doesn't.Foresheet
You mean, the part A of your question? It doesn't work because type parameters are invariant. If you try the same with trait A[+T] you're gonna get the same result.Warpath
S
-1

In your example :

scala> def baz(bs: Seq[B { type T = X }] forSome { type X }) = true

tells compiler that the function baz takes a Seq of the trait B's. The trait B happens to take a type member T but as far as compiler is concerned, it's totally fine for the function baz to take a Seq of the trait B's even if the types of the B's type member T are not the same. If you want the function to baz to take a Seq of the trait B with the same type members, you need to tell the compiler as the following:

scala> def baz[X](bs: Seq[B { type T = X }]) = true
baz: [X](bs: Seq[B{type T = X}])Boolean


scala> baz[Int](Seq(new B { type T = Int }, new B { type T = String }))
    <console>:10: error: type mismatch;
    found   : java.lang.Object with B
    required: B{type T = Int}
              baz[Int](Seq(new B { type T = Int }, new B { type T = String }))
                                               ^

scala> baz[Int](Seq(new B { type T = Int }, new B { type T = Int }))
res10: Boolean = true
Schilit answered 21/8, 2012 at 2:59 Comment(1)
Right—I understand that this is what the compiler does, but why? It seems inconsistent, for the reasons I outlined in the question. And sometimes you don't care about X (apart from knowing that it's the same) and it would be nice to have the types reflect that.Foresheet

© 2022 - 2024 — McMap. All rights reserved.