I found out reading the spec that scala supports binding type variables when doing a type pattern match:
Map(1 -> "one", 2 -> "two") match {
case l: Map[k, v] =>
// binds k to Int and v to String
// k and v are types as shown here:
val i: Iterator[Tuple2[k, v]] = l.iterator
println(i.mkString(", "))
}
Are there any fancy things or practical things I can do with this? Or binding type variables is only useful for type documentation purpose?
It occurred to me that Scala sometimes needs type annotations, such as defining function, so I tried:
def prepender(obj: Any) = obj match {
case xs: List[a] => (x: a) => x :: xs
case opt: Some[a] => (x: a) => x :: Nil
}
But then the type of the return function is weird:
prepender: (obj: Any)a with a => List[Any] forSome { type a; type a }
scala> val p = prepender(List(1,2))
p: a with a => List[Any] forSome { type a; type a } = <function1>
scala> p(1)
<console>:10: error: type mismatch;
found : Int(1)
required: a(in value res7) with (some other)a(in value res7) where
type (some other)a(in value res7), type a(in value res7)
I hope this won’t get too long, but I seriously doubt it, that’s why I’m gonna try to provide a quick answer first: “When you name (abstract) something, the main use case is referring to it later”. Well that wasn’t helpful now, was it?
Consider this simple Scala function:
The compiler does not need to know that
ais anaandbis ab. All it needs to know thataas well asbare of typeIntand thatacomes beforeb(which wouldn’t matter in this case since addition is commutative, but the compiler cares anyway!). Scala offers a (don’t get me wrong I also love it) compiler friendly placeholder syntax, which acts as a proof of this “hypothesis”.Now take a look at this:
When you don’t care about the type argument use the placeholder syntax. When you do (for whatever reason) care you should name the type argument with a lower case so the compiler knows you want to treat it as an identifier.
Back to your question.
The primary use for existential types is working around Java’s wildcard types.
This is taken from Programming in Scala – Existential Types and was slightly modified by yours truly.
Ok, so what just happened? Simple generics, no magic there?! If you are dealing with generics on a day to day basis this looks normal to you, but you are forgetting, that the ultra super concept of introducing type arguments into scope works only on classes and methods. What if you are outside of a class or a method, just in some random scope in the middle of nowhere (like REPL)? Or what if you are in a class or a method but the type arguments have not been introduced into their scopes? This is where your question and this answer come in play.
The identifier
kindis required so the compiler can verify that you are referring to the same thing.Note, that you can’t just add strings into the
setsince the type of thesetisSet[_].