Skip to content
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

Trait alias. #1733

Merged
merged 30 commits into from
Apr 24, 2017
Merged
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
55de6a3
Trait alias.
hadronized Aug 31, 2016
c0c4834
Updated the proposal according to the discussion in #1733.
hadronized Dec 24, 2016
cf74fd5
Fixed typo.
hadronized Dec 25, 2016
042b8e4
Strengthened the RFC with comments from people on the PR.
hadronized Feb 15, 2017
88d3074
Update 0000-trait-alias.md
durka Mar 6, 2017
5d0b4fe
Merge pull request #1 from durka/patch-9
hadronized Mar 6, 2017
819d8f9
fix object safety section
durka Mar 6, 2017
8a72471
just one lifetime
durka Mar 6, 2017
554c4c1
Merge pull request #2 from durka/patch-10
hadronized Mar 6, 2017
7b63d30
Added a teaching section and question about hidden free type variables.
hadronized Mar 7, 2017
6601146
Fixed teaching example – spotted by @eddyb.
hadronized Mar 7, 2017
b1be75b
Removed a question from the unresolved ones.
hadronized Mar 7, 2017
90ce5c3
Trait alias summary updated.
hadronized Mar 7, 2017
f04d4b2
Moved trait alias type vs. trait keyword in alt. section.
hadronized Mar 7, 2017
f269f9a
Added issue reference to trait alias RFC.
hadronized Mar 7, 2017
3fc5568
Fixed a typo in the teaching section of the trait-alias RFC.
hadronized Mar 7, 2017
8182509
Hygiene.
hadronized Mar 22, 2017
83322de
Refactoring a little bit and added an unresolved question about trait…
hadronized Mar 22, 2017
310c7fe
Proposal for pre-condition & implication bounds.
hadronized Mar 22, 2017
eb24a49
Typo.
hadronized Mar 22, 2017
4d99e23
Syntax for Trait<Item = AssociatedType>.
hadronized Mar 22, 2017
54ca931
Typo.
hadronized Mar 22, 2017
bf3414d
expanded detailed design text to include explicit examples of paramet…
pnkfelix Apr 13, 2017
80dbc13
Explicitly point out alternatives to `trait Alias = where PREDICATES;`
pnkfelix Apr 13, 2017
a2f8020
Add section pointing out how associated item ambiguity is handled.
pnkfelix Apr 13, 2017
dcec407
Spell out that it is an error to override an equivalence constraint.
pnkfelix Apr 13, 2017
b5ff949
Merge pull request #3 from pnkfelix/add-parametric-trait-alias-examples
hadronized Apr 13, 2017
2b14c14
Merge pull request #4 from pnkfelix/alternatives-to-equals-where
hadronized Apr 13, 2017
5c94f49
Merge pull request #5 from pnkfelix/mult-unbound-assoc-types-with-sam…
hadronized Apr 13, 2017
2a1a5b2
Merge pull request #6 from pnkfelix/associated-type-rebinding
hadronized Apr 16, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions text/0000-trait-alias.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
- Feature Name: Trait alias
- Start Date: 2016-08-31
- RFC PR:
- Rust Issue:

# Summary
[summary]: #summary

Traits can be aliased the same way types can be aliased with the `type` keyword.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this summary still accurate? I think it should be s/type keyword/trait keyword/, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The initial intention was to state that we can have alias the same way we already have with the type keyword and types. But you’re right, it’s confusing. I’m updating that.


# Motivation
[motivation]: #motivation

Sometimes, some traits are defined with parameters. For instance:

```
trait Foo<T> {
// ...
}
```

It’s not uncommon to do that in *generic* crates and implement them in *backend* crates, where the
`T` template parameter gets substituted with a *backend* type.

If someone wants to write `Foo` code and be compatible with all backends, then they will use the
*generic* crate’s `Foo<_>`. However, if someone wants to freeze the backend and keep using the same
one for the whole project but want to keep the ease of backend switching, a good practice is that
the backends should exporte a specific version of the trait so that it’s possible to use `Foo`
instead of the more explicit and unwanted `Foo<BackendType>`.

# Detailed design
[design]: #detailed-design

The idea is to add a new keyword or construct for enabling trait aliasing. One shouldn’t use the
`type` keyword as a trait is not a type and that could be very confusing.

The `trait TraitAlias as Trait` is suggested as a starter construct for the discussion.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not trait TraitAlias = Trait similar to the type keyword's syntax?
If I came across trait Foo as Bar I would probably read it as Bar being an alias for Foo.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the = syntax to mirror type aliases as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think it’s a good idea as well, as would read the other way around, yeah.


```
mod gen {
trait Foo<T> { }
}

mod backend_0 {
struct Bck0 {}

trait Foo as gen::Foo<Bck0>;
}
```

# Drawbacks
[drawbacks]: #drawbacks

The syntax `trait TraitAlias as Trait` makes parsers need a lookhead (`=` or `as`?).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this should say = as well (or move the as syntax to Alternatives).


# Alternatives
[alternatives]: #alternatives

A keyword was planned, like `alias`:

```
alias Foo = gen::Foo<Bck0>;
```

However, it’s not a good idea as it might clash with already used `alias` in codebases.

# Unresolved questions
[unresolved]: #unresolved-questions

The syntax `trait TraitAlias as Trait` is not yet stabilized and needs to be discussed.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be changed to = as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo fixed, but shall we just remove that part?