Why are the bounds of type parameters ignored when using existential types in Scala?

What I mean is this:

scala> class Bounded[T <: String](val t: T)
defined class Bounded

scala> val b: Bounded[_] = new Bounded("some string")
b: Bounded[_] = Bounded@2b0a141e

scala> b.t
res0: Any = some string

Why does res0 have type Any and not String? It sure could know that b.t is at least a String. Writing

val b: Bounded[_ <: String] = new Bounded("some string")

works, but it is redundant with respect to the declaration of the class itself.

Answers


First, I have edited the question title. You are not using dependent types, which Scala doesn't have anyway, but existential types. Second, you are not inferring anything, you are explicitly declaring the type.

Now, if you did write Bounded[Any], Scala wouldn't let you. However, one of the uses of existential types is to deal with situations where the type parameter is completely unknown -- such as Java raw types, where.

So my guess is that making an exception in a situation that seems obvious enough will break some other situation where existential type is the only way to deal with something.


Need Your Help

Flex column replacement for unsupported browsers

css flexbox

I've been trying to get my head managing a specific layout without using flexbox (specifically flex-direction: column). I'm almost certain this has been asked elsewhere but for the life of me I hav...

Renaming files based on its folder name

powershell rename

How do I rename files that is based on its folder name? In a current folder it has those folder names:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.