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

Generics details 8: interface default and final members #990

Merged
merged 22 commits into from
Jan 21, 2022
Merged

Generics details 8: interface default and final members #990

merged 22 commits into from
Jan 21, 2022

Conversation

josh11b
Copy link
Contributor

@josh11b josh11b commented Dec 15, 2021

Allowing interfaces to define default values for its associated entities. This:

  • Helps with evolution by reducing the changes needed to add new members to an interface.
  • Reduces boilerplate when some value is more common than others.
  • Addresses the gap between the minimum necessary for a type to provide the desired functionality of an interface and the breadth of API that user's desire.

As an alternative, final values can be provided instead, which can't be overridden, but are more predictable for users and may avoid dynamic dispatch overhead in some cases.

Example:

// Interface parameter has a default of `Self`
interface Add(Right:! Type = Self) {
  // `AddWith` *always* equals `Right`
  final let AddWith:! Type = Right;
  // `Result` has a default of `Self`
  let Result:! Type = Self;
  fn DoAdd[me: Self](right: Right) -> Result;
}

impl String as Add() {
  // Right == AddWith == Result == Self == String
  fn DoAdd[me: Self](right: Self) -> Self;
}

@josh11b josh11b added the proposal A proposal label Dec 15, 2021
@josh11b josh11b added this to Draft in Proposals via automation Dec 15, 2021
@josh11b josh11b requested a review from a team December 15, 2021 19:16
@josh11b josh11b changed the title Generics details 8: interface defaults Generics details 8: interface default and final members Dec 16, 2021
@josh11b josh11b marked this pull request as ready for review December 16, 2021 00:18
@josh11b josh11b requested a review from a team as a code owner December 16, 2021 00:18
@josh11b josh11b moved this from Draft to RFC in Proposals Dec 16, 2021
@github-actions github-actions bot added the proposal rfc Proposal with request-for-comment sent out label Dec 16, 2021
Copy link
Contributor

@zygoloid zygoloid left a comment

Choose a reason for hiding this comment

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

This looks great. I think final defaults may need a little more discussion before I'd consider us to have consensus here, but the rest looks good to go to me.

docs/design/generics/details.md Outdated Show resolved Hide resolved
docs/design/generics/details.md Outdated Show resolved Hide resolved
docs/design/generics/details.md Show resolved Hide resolved
docs/design/generics/details.md Show resolved Hide resolved
@josh11b
Copy link
Contributor Author

josh11b commented Jan 7, 2022

I added some text and made some small reorganization based on recent discussion:

7c9c95d

Copy link
Contributor

@zygoloid zygoloid left a comment

Choose a reason for hiding this comment

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

Looks good to me. Soliciting more opinions before approving.

@chandlerc
Copy link
Contributor

Generally makes sense. Minor comment about the design doc, but not at all blocking or even really interesting. Just a. doc improvement comment.

Copy link
Contributor

@chandlerc chandlerc left a comment

Choose a reason for hiding this comment

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

(now actually posting the pending comment)

[associated constants](https://doc.rust-lang.org/reference/items/associated-items.html#associated-constants-examples).
Rust has found them valuable.

### `final` members
Copy link
Contributor

Choose a reason for hiding this comment

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

Super minor (and fine for a follow-up): there isn't much motivation here in the design for this feature compared to others. The motivation in the proposal makes sense to me, but maybe three is a way to briefly summarize that so it doesn't read like an omission here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in 620d6de

@josh11b josh11b merged commit 3e0b44a into carbon-language:trunk Jan 21, 2022
Proposals automation moved this from RFC to Accepted Jan 21, 2022
@josh11b josh11b deleted the defaults branch January 21, 2022 22:10
@github-actions github-actions bot added proposal accepted Decision made, proposal accepted and removed proposal rfc Proposal with request-for-comment sent out labels Jan 21, 2022
chandlerc pushed a commit that referenced this pull request Jun 28, 2022
Allowing interfaces to define default values for its associated entities. This:

-   Helps with evolution by reducing the changes needed to add new members to an interface.
-   Reduces boilerplate when some value is more common than others.
-   Addresses the gap between the minimum necessary for a type to provide the desired functionality of an interface and the breadth of API that user's desire.

As an alternative, final values can be provided instead, which can't be overridden, but are more predictable for users and may avoid dynamic dispatch overhead in some cases.

Example:
```
// Interface parameter has a default of `Self`
interface Add(Right:! Type = Self) {
  // `AddWith` *always* equals `Right`
  final let AddWith:! Type = Right;
  // `Result` has a default of `Self`
  let Result:! Type = Self;
  fn DoAdd[me: Self](right: Right) -> Result;
}

impl String as Add() {
  // Right == AddWith == Result == Self == String
  fn DoAdd[me: Self](right: Self) -> Self;
}
```

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
github-merge-queue bot pushed a commit that referenced this pull request Sep 23, 2023
Includes proposals:
- #990
- #2188
- #2138
- #2200
- #2360
- #2760
- #2964
- #3162

Also tries to use more precise language when talking about:
- implementations, to avoid confusing `impl` declaration and definitions
with the `impls` operator used in `where` clauses, an issue brought up
in #2495 and #2483;
- "binding patterns", like `x: i32`, and "bindings" like `x`.

---------

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal accepted Decision made, proposal accepted proposal A proposal
Projects
No open projects
Proposals
Accepted
Development

Successfully merging this pull request may close these issues.

None yet

3 participants