An opaque type should not be covariant when the underlying type is invariant
See original GitHub issueCompiler version
3.1.0
Minimized code
opaque type CovariantArray[+A] = Array[A]
object CovariantArray:
def crash() =
val stringArray: CovariantArray[String] = Array("foo", "bar")
val anyArray: CovariantArray[Any] = stringArray
anyArray(0) = 42
stringArray(0).length
CovariantArray.crash()
Output
java.lang.ArrayStoreException: java.lang.Integer
at Playground$CovariantArray$.crash(main.scala:9)
at Playground$.<clinit>(main.scala:12)
... 13 more
Expectation
Should not compile
Issue Analytics
- State:
- Created 2 years ago
- Comments:12 (7 by maintainers)
Top Results From Across the Web
Support Covariant Opaques (documentation) · Issue #5240 · erlang ...
"Types declared as opaque represent sets of terms whose structure is not supposed to be visible from outside of their defining module. That...
Read more >SE-0309: Unlock existential types for all protocols - Swift Forums
Is there a general guide/rule somewhere that I can read up on what positions are considered by the type constructor as covariant, invariant,...
Read more >Property declares an opaque return type, but has no initializer ...
I get the error Property declares an opaque return type, but has no initializer expression from which to infer an underlying type ....
Read more >Covariance and Contravariance in Generics - Microsoft Learn
An invariant generic type parameter is neither covariant nor contravariant. ... type. Variance does not apply to delegate combination.
Read more >Type Lambdas - More Details - Scala 3
Abstract types and opaque type aliases remember the variances they were ... known to be invariant (and not covariant, as its right-hand side...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I don’t see a bug, putting it after (
Array[_ <: A @scala.annotation.unchecked.uncheckedVariance]
) still annotates the type argument, not its upper bound.I thought of something like