-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Conversation
There was a problem hiding this 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.
I added some text and made some small reorganization based on recent discussion: |
There was a problem hiding this 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.
Generally makes sense. Minor comment about the design doc, but not at all blocking or even really interesting. Just a. doc improvement comment. |
There was a problem hiding this 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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 620d6de
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 <[email protected]>
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 <[email protected]>
Allowing interfaces to define default values for its associated entities. This:
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: