-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Newtype deriving #508
Newtype deriving #508
Conversation
These wrapper types are tuple structs, and tuples already have default implementations for all the ops. The auto-derived impls for ops on tuple structs could trivially be implemented in terms of regular tuples, though it would need a lot of the same machinery and would only differ in the method bodies, unless there is a way to make a tuple struct desugar to a regular tuple. |
There are a lot of edge cases not covered by this RFC - what about traits which make references to |
Another thought: since macro syntax supports patterns, the boilerplate of implementing ops for tuple structs could trivially be implemented as a contextual macro. I'll work on an example later. Though, deriving the ops for a struct where its fields implement the op in question should work the same way as how the rest of the derivable traits are implemented, though it would have to assume a RHS and Result of |
See also discussion on RFC #186 , which was postponed with tracking issue #261 (the titles of both are a little bit misleading; its not just about a keyword, but rather about feature much like the one described here). (Update: of course this is slightly different, in that it is proposing leveraging the macro/attribute system to derive the desired implementations here, which is more along the lines of what I wanted to see happen for the "newtype" feature request.) |
@reem Could you give a concrete example of a problem when a trait refers to Clone is actually an easy one. As of today, you can write UPDATE: I see now that the implementation of traits with generic arguments is not trivial as I thought when I wrote the RFC. I will think about it and try to improve the description of the design. |
@cybergeek94 Are you sure tuples already have default implementations for all the ops? It doesn't seem to work for me. |
@aochagavia I and @eddyb came across issues with Restricting the RfC to the ops works as well. |
|
||
In case the first approach is chosen, the traits available to be derived would be: | ||
* All traits in `std::ops` | ||
* Show |
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.
Show
can be derived via #[deriving(Show)]
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.
While this is true, maybe you are only be interested in the wrapped value (e.g. showing "2.0"
instead of "Centimeters(2.0)"
). In that case it is useful to be able to derive Show
.
Thanks for the PR @aochagavia! As @pnkfelix mentioned this issue is currently being tracked in #261 for the concept of having a newtype also export all of the underlying traits. At this time features like this sadly aren't making the cut for 1.0 as we're trying to stabilize the language and the libraries in preparation. I'm going to close this RFC for now, but I encourage you to continue discussion on the issue if you'd like so we can hopefully reach a solution for this after 1.0. Thanks again for taking the time to write this up though! |
Rendered
See also the discussion on rust-lang/rust#19597
cc @aturon @huonw