-
Notifications
You must be signed in to change notification settings - Fork 13.3k
impl on type aliases #32357
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
Comments
/cc @rust-lang/lang , is this a feature that's desired? And if so, would it require an RFC? |
A type alias is just that: An alias, not a different type. If you want to create a different type, use the newtype pattern for that. |
@jonas-schievink but the newtype pattern doesn't inherit all of the aliased types methods. Is there a reason that |
@calebmer Yes, it is. Because everyone using |
This would require an RFC and would be better served as an issue on that repo rather than here. Having said that, I don't believe this is desirable. The preferred approach is to use newtypes (as @jonas-schievink mentions). A possible solution is 'newtype deriving' where a newtype inherits impls. That has been discussed a fair amount before, not sure if there is an issue open for it. |
I would like to see an issue on newtype deriving if it exists.
|
Indeed, as @nrc noted, this would most definitely require an RFC, but I think more importantly it's not really something we would want. Type aliases, indeed, are just aliases. Some kind of lighter-weight deriving for struct wrappers (newtypes) would be nice though and would... probably... fill the use-case here. Therefore, I'm going to close this issue in favor of rust-lang/rfcs#479. |
To provide a nice interface to users I would like to be able to add some
impl
functions to atype
alias. For example, I have the following code:I would like to add the following:
So the following would work:
But this would not:
Another use case is I have a map representing a JSON object (
BTreeMap<Key, Value>
) that I would like to add custom methods to without having to write a struct that redefines all of theBTreeMap
methods. This currently cannot be done.As a side note, this feels a little like classical inheritance…
The text was updated successfully, but these errors were encountered: