question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

An opaque type should not be covariant when the underlying type is invariant

See original GitHub issue

Compiler 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:closed
  • Created 2 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
dwijnandcommented, Nov 30, 2021

OK I thought one would need to put the annotation on the wildcard (which doesn’t seem to be possible BTW - is this a bug?)

I don’t see a bug, putting it after (Array[_ <: A @scala.annotation.unchecked.uncheckedVariance]) still annotates the type argument, not its upper bound.

0reactions
prolativcommented, Nov 30, 2021

I thought of something like

type CovariantArray[+A] = Array[_ @scala.annotation.unchecked.uncheckedVariance <: A]
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found