Skip to content

Variance of generics #295

Closed
Closed
@JukkaL

Description

@JukkaL

It should be possible to define a type variable as covariant. For example, the type variable of Iterable[t] could be covariant, and then Iterable[x] would a subtype of Iterable[y] if x is a subtype of y. Currently this is not the case, which is sometimes awkward, and this would even more awkward with union types.

Not sure about the syntax. Maybe something like this:

T = typevar('T', covariant=True)   

(Ouch, looks pretty technical.)

Covariant type variables must not appear within method argument types or instance variable types, i.e. they are only valid as return types. Also, only read-only properties can have types with covariant type variables.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions