Closed CarliJoy closed 9 months ago
@randolf-scholz I am not an expert with variance. Could you extend your example for a static function as well as a method and explain what happens with arguments/return values as well?
I believe the consistency based rule we are going for does not require we do extra work for Variance with Any that isn't already being covered more simply than this. I'll be including notes on the effective variance we expect in a few examples. IF this needs further discussion, please reopen expanding on that.
@mark-todd One has to be really careful and diligent with phrasing of these examples. In particular, we should try to avoid using the word "property" if we mean a regular attribute and not a
@property
, because it makes a huge difference whether we talk about a mutable attribute (⇝ invariant), or a read-only property (⇝ covariant).Let's take a look at a modified version of Eric's example from earlier:
Now, what is the interface of
T & Movie
?info
is a mutable attribute. Therefore,Movie
is invariant ininfo
, which logically translates to the statement:S <: Movie ⟹ type(S.info) is MovieInfo
, i.e. ifS
is a subtype ofMovie
, then the type ofS.info
must beMovieInfo
.(T & Movie).info
is of typeT.info & Movie.info
=Any & MovieInfo
.MovieInfo
due to the invariance. (The only alternative is thatT
andMovie
are incompatible)metadata
is a read-only property, so covariant, meaningS <: Movie ⟹ S.metadata <: Movie.metadata
.(T & Movie).metadata
is of typeT.metadata & Movie.metadata
=Any & Metadata
(*), which is still guaranteed to be a subtype ofMetadata
, but could have a much wider interface.Any & Metadata
cannot be simplified away in this case.So even with ④, exact inference can be made when types are invariant, and in fact this gives an alternative solution for people worried about intersection with
Any
by simply using invariant types. (Presumably one would want a way to manually mark read-only properties as invariant)(*) In principal it should be something like
Any & property[Metadata]
, but for technical reasonsproperty
isn't even generic yet in typeshed, so let's pretend for the moment it's simple a read-only, covariant attribute..._Originally posted by @randolf-scholz in https://github.com/CarliJoy/intersection_examples/issues/31#issuecomment-1881694200_