Reputation: 10303
What is the difference between X[Any]
and X[_]
?
Let's consider, for example, two functions below:
def foo(x:X[_]){}
def foo(x:X[Any]){}
What is exactly the difference between these declarations above?
Upvotes: 4
Views: 205
Reputation: 285
The difference is in insignificant
scala> class X[T]
defined class X
scala> def type_of[T](x: X[T])(implicit m: Manifest[T]) = m.toString
type_of: [T](x: X[T])(implicit m: Manifest[T])java.lang.String
scala> val x1: X[Any] = new X
x1: X[Any] = X@1a40cfc
scala> val x2: X[_] = new X
x2: X[_] = X@29d838
scala> type_of(x1)
res10: java.lang.String = Any
scala> type_of(x2)
res11: java.lang.String = _ <: Any
I can not name a situation, when you can use Any but can not use _ and vice verse.
Upvotes: -1
Reputation: 297295
The first is an existential type, and the second is a normal type. The first syntax actually means this:
def foo(x:X[t] forSome { type t }){}
What this means is that x
is of type X[t]
, where t
can be any unspecified type t
.
Intuitively, X[_]
means the type parameter of X
is irrelevant, whereas X[Any]
says it must be Any
.
Upvotes: 9