You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classAnimal{}classCatextendsAnimal{}classCont<T>{}
(new Cont<Cat> : Cont<Animal>)
The above example currently displays the following error:
Cannot cast `new Cont<...>()` to `Cont` because `Cat` [1] is incompatible with `Animal` in type argument `T`
For many people, it is quite an unintuitive error message given that the Cat type itself is a subtype of Animal.
I propose error messages which show a suggestion to change class Cont<T> to class Cont<+T>. So that:
if A <: B but C<A> </: C<B>, then an error message like "A is a subtype of B, but C is not covariant in type argument T. Did you mean to define C<+T> instead of C<T>? " should be shown.
if B <: A but C<A> </: C<B>, then an error message like "B is a subtype of A, but C is not contravariant in type argument T. Did you mean to define C<-T> instead of C<T>?" should be shown.
(</: means "not a subtype of")
Like in Scala:
Aside of generics, it also applies to object properties:
…s during array subtyping
Summary:
Arrays are in fact invariantly typed. LTI will make this clear when we no longer widen tvars. However, this can cause too many errors, and currently it's hard to distinguish this type of errors from others.
In the last diff, I did the preparation that will make this situation unambiguously detectable during error printing. In this diff, I attached additional explanations to help user know what's going on. It can help us analyze error diff as well. In the future, we can even use this information to power an autofix.
Close#8041
Changelog: [internal]
Reviewed By: panagosg7
Differential Revision: D42716180
fbshipit-source-id: 24b9282dafb37b2c420f6b7074ef9f5aac3a2bdd
Proposal
The above example currently displays the following error:
For many people, it is quite an unintuitive error message given that the
Cat
type itself is a subtype ofAnimal
.I propose error messages which show a suggestion to change
class Cont<T>
toclass Cont<+T>
. So that:A <: B
butC<A> </: C<B>
, then an error message like "A
is a subtype ofB
, butC
is not covariant in type argumentT
. Did you mean to defineC<+T>
instead ofC<T>
? " should be shown.B <: A
butC<A> </: C<B>
, then an error message like "B
is a subtype ofA
, butC
is not contravariant in type argumentT
. Did you mean to defineC<-T>
instead ofC<T>
?" should be shown.(
</:
means "not a subtype of")Like in Scala:
Aside of generics, it also applies to object properties:
Related to #3998.
The text was updated successfully, but these errors were encountered: