-
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
#[deriving(PartialEq, Eq, Show, Clone, Hash, PartialOrd, Ord, Default, Encodable, Decodable)] is too verbose #441
Comments
It would be nice if it were possible to have traits which are opt-out instead of opt-in. So, for example, if I explicitly wanted to forbid Showing a particular type (for whatever reason) I could write:
You could also allow users to define their own traits like this by providing default implementations of all methods and marking the trait with an |
Imagine how much code you would have to write in C++! More constructively, one small but potentially significant improvement would be to automatically derive supertraits; so that e.g. |
A less comprehensive change would be to add minor globbing: // These two are equivalent because `*Eq` includes `PartialEq` and `Eq`.
// Same for `*Ord`
#[deriving(*Eq, *Ord)]`
#[deriving(PartialEq, Eq, PartialOrd, Ord)] Deriving supertraits is probably a better idea though. |
@barosl That is definitely amusing and I would not be opposed. Kinda magical. |
👍 It would be nice if there was a way to distinguish between traits and supertraits (such as a some sign or a naming convention) |
This seems like another use case for macros-in-attributes: macro_rules! ord_traits {
() => (PartialEq, Eq, PartialOrd, Ord)
}
#[deriving(ord_traits!())]
pub enum MyEnum { ... } |
Unsurprisingly Haskell people is also exploring options regarding this. If curious one can check out their points on it: https://ghc.haskell.org/trac/ghc/wiki/IntrinsicSuperclasses |
If I have a small struct wrapping some basic types, or a basic enum, it's quite likely that the entire list of traits above is appropriate to derive (with the possible exception of Default). However, this is so noisy that, in practice, nobody seems to do so, including in the main Rust repository: for example, grep libsyntax for
pub enum
and look at how many of them cannot be used as keys in maps or even Shown. Instead, some random subset of traits is derived depending on what people actually need to do with the struct. But this is annoying:The most obvious solution would be a shortcut to derive as many traits as possible, though I'm not sure how that would interact with new derivable traits potentially being added in the future.
The text was updated successfully, but these errors were encountered: