What is the difference between List[T] forSome {type T}
and List[T forSome {type T}]
? How do I read them in "English"? How should I grok the forSome
keyword? What are some practical uses of forSome
? What are some useful practical and more complex than simple T forSome {type T}
usages?
Attention: (Update 2016-12-08) The forSome keyword is very likely going away with Scala 2.13 or 2.14, according to Martin Odersky's talk on the ScalaX 2016. Replace it with path dependent types or with anonymous type attributes (A[_]
). This is possible in most cases. If you have an edge case where it is not possible, refactor your code or loosen your type restrictions.
Usually when you use a generic API, the API guarantees you, that it will work with any type you provide (up to some given constraints). So when you use List[T]
, the List API guarantees you that it will work with any type T
you provide.
With forSome
(so called existentially quantified type parameters) it is the other way round. The API will provide a type (not you) and it guarantees you, it will work with this type it provided you. The semantics is, that a concrete object will give you something of type T
. The same object will also accept the things it provided you. But no other object may work with these T
s and no other object can provide you with something of type T
.
The idea of "existentially quantified" is: There exists (at least) one type T
(in the implementation) to fulfill the contract of the API. But I won't tell you which type it is.
forSome
can be read similar: For some types T
the API contract holds true. But it is not necessary true for all types T
. So when you provide some type T
(instead of the one hidden in the implementation of the API), the compiler cannot guarantee that you got the right T
. So it will throw a type error.
So when you see List[T] forSome {type T}
in an API, you can read it like this: The API will provide you with a List
of some unknown type T
. It will gladly accept this list back and it will work with it. But it won't tell you, what T
is. But you know at least, that all elements of the list are of the same type T
.
The second one is a little bit more tricky. Again the API will provide you with a List
. And it will use some type T
and not tell you what T
is. But it is free to choose a different type for each element. A real world API would establish some constraints for T
, so it can actually work with the elements of the list.
forSome
is useful, when you write an API, where each object represents an implementation of the API. Each implementation will provide you with some objects and will accept these objects back. But you can neither mix objects from different implementations nor can you create the objects yourself. Instead you must always use the corresponding API functions to get some objects that will work with that API. forSome
enables a very strict kind of encapsulation. You can read forSome
in the following way:
The API contract folds true for some types. But you don't know for which types it holds true. Hence you cannot provide you own type and you cannot create your own objects. You have to use the ones provided through the API that uses
forSome
.
This is quite informal and might even be wrong in some corner cases. But it should help you to grok the concept.
There are a lot of questions here, and most of them have been addressed pretty thoroughly in the answers linked in the comments above, so I'll respond to your more concrete first question.
There's no real meaningful difference between List[T] forSome { type T }
and List[T forSome { type T }]
, but we can see a difference between the following two types:
class Foo[A] type Outer = List[Foo[T]] forSome { type T } type Inner = List[Foo[T] forSome { type T }]
We can read the first as "a list of foos of T
, for some type T
". There's a single T
for the entire list. The second, on the other hand, can be read as "a list of foos, where each foo is of T
for some T
".
To put it another way, if we've got a list outer: Outer
, we can say that "there exists some type T
such that outer
is a list of foos of T
", where for a list of type Inner
, we can only say that "for each element of the list, there exists some T
such that that element is a foo of T
". The latter is weaker—it tells us less about the list.
So, for example, if we have the following two lists:
val inner: Inner = List(new Foo[Char], new Foo[Int]) val outer: Outer = List(new Foo[Char], new Foo[Int])
The first will compile just fine—each element of the list is a Foo[T]
for some T
. The second won't compile, since there's not some T
such that each element of the list is a Foo[T]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With