Skip to content

Add declaration-site variance feature specification - #1230

Merged
eernstg merged 22 commits into
mainfrom
spec_dcl_site_variance_sep20
Aug 21, 2026
Merged

Add declaration-site variance feature specification#1230
eernstg merged 22 commits into
mainfrom
spec_dcl_site_variance_sep20

Conversation

@eernstg

@eernstg eernstg commented Sep 21, 2020

Copy link
Copy Markdown
Member

This PR adds a feature specification for declaration-site variance, which is a sound and explicit mechanism for specifying that a type parameter of a generic class can be covariant (marked by out), invariant (inout), or contravariant (in), cf. #524.

Comment thread accepted/future-releases/variance/feature-specification.md Outdated
Comment thread accepted/future-releases/variance/feature-specification.md Outdated
Comment thread accepted/future-releases/variance/feature-specification.md Outdated
Comment thread accepted/future-releases/variance/feature-specification.md Outdated
Comment thread working/0524-variance/feature-specification.md Outdated
Comment thread working/0524-variance/feature-specification.md
Comment thread accepted/future-releases/variance/feature-specification.md Outdated
Comment thread working/0524-variance/feature-specification.md
Comment thread working/0524-variance/feature-specification.md Outdated
Comment thread working/0524-variance/feature-specification.md Outdated
Comment thread accepted/future-releases/variance/feature-specification.md Outdated
@eernstg
eernstg force-pushed the spec_dcl_site_variance_sep20 branch from 9b92852 to b944a7b Compare January 27, 2022 13:45

@eernstg eernstg left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review response.

Comment thread accepted/future-releases/variance/feature-specification.md Outdated
Comment thread accepted/future-releases/variance/feature-specification.md Outdated
Comment thread accepted/future-releases/variance/feature-specification.md Outdated
Comment thread working/0524-variance/feature-specification.md Outdated
Comment thread working/0524-variance/feature-specification.md Outdated
Comment thread accepted/future-releases/variance/feature-specification.md Outdated
Comment thread working/0524-variance/feature-specification.md Outdated
Comment thread working/0524-variance/feature-specification.md Outdated
Comment thread working/0524-variance/feature-specification.md
Comment thread accepted/future-releases/variance/feature-specification.md Outdated
@kallentu

kallentu commented Aug 6, 2026

Copy link
Copy Markdown
Member

@eernstg What's the status of this PR? Could we merge this into the language repo?

@eernstg

eernstg commented Aug 7, 2026

Copy link
Copy Markdown
Member Author

@kallentu wrote:

What's the status of this PR? Could we merge this into the language repo?

I haven't worked on this PR recently, but it could be useful to make sure it is up to date and then use that during the subsequent finalization of the feature.

@leafpetersen, WDYT? Do you want to start afresh on a specification of declaration-site variance, or do we proceed to do it based on this PR?

Comment thread working/0524-variance/feature-specification.md Outdated
Comment thread working/0524-variance/feature-specification.md
Comment thread working/0524-variance/feature-specification.md Outdated
Comment thread working/0524-variance/feature-specification.md Outdated
Comment thread working/0524-variance/feature-specification.md
Comment thread working/0524-variance/feature-specification.md
Comment thread working/0524-variance/feature-specification.md Outdated
Comment thread working/0524-variance/feature-specification.md Outdated
Comment thread working/0524-variance/feature-specification.md Outdated
@lrhn

lrhn commented Aug 10, 2026

Copy link
Copy Markdown
Member

I think this specification can probably be used as the basis.

My biggest worry is how to migrate platform libraries.

If we can't make Future, Stream and Iterable be covariant, we have a problem.
(So we have a problem in reduce. Can we deprecate and remove it, and/or replace it with an extension method? Or introduce statically typed instance members, aka extension members declared on the type iteself?)

We need some migration path that allows adding restrictions to platform libraries.
(I'd love if it's not just another "platform library exception" that makes old libraries not see the new types, that means we can't get any benefits from the change.)

@eernstg
eernstg force-pushed the spec_dcl_site_variance_sep20 branch from 5b1e3c6 to 6157eb2 Compare August 20, 2026 09:07
@eernstg

eernstg commented Aug 21, 2026

Copy link
Copy Markdown
Member Author

We're moving on this one! 🎉

I'll land this document now. Leaf and I talked about a couple of questions in need of further discussion, and they're on the language team agenda for the next meeting. Further discussions and revisions will occur as needed.

@eernstg
eernstg merged commit 23ce49f into main Aug 21, 2026
9 checks passed
@eernstg
eernstg deleted the spec_dcl_site_variance_sep20 branch August 21, 2026 07:49
- **Interface Compositionality**: `T0` is an interface type or an extension
type `C0<S0, ..., Sk>` and `T1` is `C0<U0, ..., Uk>`. For `i` in `0..k`,
let `vi` be the declared variance of the `i`th type parameter of
`C0`. Then, for each `i` in `0..k`, one of the following holds:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this sentence be "Then, T0 <: T1 if and only if for each ..."? I feel like it's missing what these conditions define.


It is a compile-time error if a variance modifier is specified for a type
parameter declared in the type parameter list of a declaration of an
extension, a generic function or method, an extension type, or a type

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the top, it says

The explicitly declared variance can be used with [...], that is, class, mixin class,
enum, extension type, and mixin declarations.

Here, it says that It's a compile time error with extension types.
Should they be removed from one of the lists?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants