I am experimenting with existential types.
I was playing with a function that expects a sequence where the elements of that seq are all the same type. I had ..
def bar[X](as: Seq[A[X]]) = true
Where ...
// parametised type to use in the question
trait A[T]
I then came across the "forSome" syntax and found I could achieve the same constraint with it.
I wrote the following for comparison purposes ...
// useful types
trait A[T]
class AI extends A[Int]
class AS extends A[String]
// define two functions that both have the same constraint.
// ie the arg must be a Sequence with all elements of the same parameterised type
def foo(as: Seq[A[X]] forSome { type X }) = true
def bar[X](as: Seq[A[X]]) = true
// these compile because all the elements are the same type (AI)
foo(Seq(new AI, new AI))
bar(Seq(new AI, new AI))
// both these fail compilation as expected because
// the X param of X[A] is different (AS vs AI)
foo(Seq(new AI, new AS))
bar(Seq(new AI, new AS))
What I am trying to understand is - am I missing something? What is the benefit of one signature over the other.
One obvious diff is that the compilation errors are different.
scala> foo(Seq(new AI, new AS))
<console>:12: error: type mismatch;
found : Seq[A[_ >: String with Int]]
required: Seq[A[X]] forSome { type X }
foo(Seq(new AI, new AS))
^
scala> bar(Seq(new AI, new AS))
<console>:12: error: no type parameters for method bar: (as: Seq[A[X]])Boolean e
xist so that it can be applied to arguments (Seq[A[_ >: String with Int]])
--- because ---
argument expression's type is not compatible with formal parameter type;
found : Seq[A[_ >: String with Int]]
required: Seq[A[?X]]
bar(Seq(new AI, new AS))
^
<console>:12: error: type mismatch;
found : Seq[A[_ >: String with Int]]
required: Seq[A[X]]
bar(Seq(new AI, new AS))
^
scala>
The difference is that in foo
you may not refer to type X
, whereas in bar
you can:
// fails
def foo(as: Seq[A[X]] forSome { type X }) = Set.empty[X]
// btw the same:
def foo(as: Seq[A[_]]) = Set.empty[???] // <-- what would you put here?
// OK
def bar[X](as: Seq[A[X]]) = Set.empty[X]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.