Skip to content

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

Closed
calebmer opened this issue Mar 19, 2016 · 7 comments
Closed

impl on type aliases #32357

calebmer opened this issue Mar 19, 2016 · 7 comments
Labels
A-type-system Area: Type system

Comments

@calebmer
Copy link

To provide a nice interface to users I would like to be able to add some impl functions to a type alias. For example, I have the following code:

type Schema = Box<SchemaTrait + 'static>;

trait SchemaTrait {}

struct SchemaNumber {}
impl SchemaTrait for SchemaNumber {}

I would like to add the following:

impl Schema {
  fn new_number() -> Schema {
    Box::new(SchemaNumber::new())
  }
}

So the following would work:

Schema::new_number()

But this would not:

Box::new_number() // Fails, method is only for the type alias.

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 the BTreeMap methods. This currently cannot be done.

As a side note, this feels a little like classical inheritance…

@steveklabnik steveklabnik added the A-type-system Area: Type system label Mar 19, 2016
@steveklabnik
Copy link
Member

/cc @rust-lang/lang , is this a feature that's desired? And if so, would it require an RFC?

@jonas-schievink
Copy link
Contributor

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.

@calebmer
Copy link
Author

@jonas-schievink but the newtype pattern doesn't inherit all of the aliased types methods. Is there a reason that type couldn't create a new type?

@KalitaAlexey
Copy link
Contributor

@calebmer Yes, it is. Because everyone using type as alias.

@nrc
Copy link
Member

nrc commented Mar 20, 2016

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.

@calebmer
Copy link
Author

I would like to see an issue on newtype deriving if it exists.
On Sun, Mar 20, 2016 at 17:34 Nick Cameron [email protected] wrote:

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
https://github.com/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.


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#32357 (comment)

@nikomatsakis
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-type-system Area: Type system
Projects
None yet
Development

No branches or pull requests

6 participants