Why are parameter type boundaries ignored when using existential types in Scala?

What I mean:

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 is res0 of type Any, not String? He probably knew that bt is at least String. Record

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

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

+3
source share
2 answers

First, I edited the title of the question. You are not using dependent types that Scala does not have anyway, but existential types. Secondly, you cannot stand anything, you explicitly declare the type.

, Bounded[Any], Scala . , , - , Java, .

, , , , - -.

+5

, "Stickyness" Wildcards.

, , , Bounded[_] ( Bounded[$1] forSome { type $1 }), .

@extempore :)

. "yellow " ! , - , .

+2

Source: https://habr.com/ru/post/1777876/


All Articles