Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variance of generics #295

Closed
JukkaL opened this issue Jul 15, 2014 · 4 comments
Closed

Variance of generics #295

JukkaL opened this issue Jul 15, 2014 · 4 comments
Labels

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Jul 15, 2014

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.

@gvanrossum
Copy link
Member

And what about contravariance?

@JukkaL
Copy link
Collaborator Author

JukkaL commented Aug 9, 2015

#694 added basic support for covariance and contravariance, but there's some more work to be done. I'll add links to futher issues here.

@JukkaL
Copy link
Collaborator Author

JukkaL commented Aug 9, 2015

Remaining subtasks: #734, #735, #736, #737

There may be more, but the above would cover a lot of interesting cases.

@JelleZijlstra
Copy link
Member

Variance in typevars has been supported for a long time. Closing in favor of the more specific bugs Jukka linked to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants