(it's a repost of my unanswered question from scala-user@googlegroups.com about Scala)

In the Scala Language Specification, §4.4 Type Parameters, there is a requirement:

The most general form of a first-order type parameter is @a1 ... @an ± t >: L <: U. Here, L and U are lower and upper bounds that constrain possible type arguments for the parameter. It is a compile-time error if L does not conform to U.

I'm interested in a particular case of this rule, when it applies to type parameters of a generic class definition.

IIRC, in early versions of Scala this rule did not exist. One might think this was benign and in worst case could only result in an uninstantiable and uninhabited type (and even could be useful in some cases, when L and U depend on some type parameters and can be simultaneously satisfied with certain substitutions, although statically L does not conform to U).

But, as I recall, it was demonstrated that without this rule it was possible to compile certain non-typesafe program, exploiting transitivity of subtyping, that resulted in a ClassCastException at runtime, although no explicit casts were involved. I tried to find or reconstruct such an example, but so far without success.

Could you please help me to find such an example, or point me to other reasons why this rule must be required for the language to be typesafe? Thanks!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.