Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upTrait alias. #1733
Conversation
mcarton
reviewed
Aug 31, 2016
| 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. |
This comment has been minimized.
This comment has been minimized.
mcarton
Aug 31, 2016
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.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
phaazon
Sep 1, 2016
Author
Contributor
Yeah, I think it’s a good idea as well, as would read the other way around, yeah.
This comment has been minimized.
This comment has been minimized.
|
Note that this is sugar for trait Foo: gen::Foo<Bck0> {}
impl<T: gen::Foo<Bck0>> Foo for T {}So since the functionality is already supported, it makes sense to add syntax. The |
nrc
added
the
T-lang
label
Aug 31, 2016
This comment has been minimized.
This comment has been minimized.
|
@durka, hm, are you sure it safely desugars to that? It creates a brand new trait, not really an alias. |
This comment has been minimized.
This comment has been minimized.
|
@durka You can't implement |
This comment has been minimized.
This comment has been minimized.
|
You're right, the trait aliasing that I demonstrated only works for bounds. I didn't realize that this RFC would allow impl blocks as well. The RFC could benefit from an example! |
This comment has been minimized.
This comment has been minimized.
|
@durka More importantly, the I prefer the Also, will this be able to support multiple traits, ie. can I do |
This comment has been minimized.
This comment has been minimized.
|
I think |
This comment has been minimized.
This comment has been minimized.
|
The thing about In the simpler case of But Rust currently doesn't have any way to make a trait object of two non-builtin traits, like Sure, we could have the same syntax do two different things, but that seems weird. It seems to me that for now we should just do |
This comment has been minimized.
This comment has been minimized.
|
@durka You're forgetting that traits are not just used as trait objects. |
This comment has been minimized.
This comment has been minimized.
|
@Diggsey I didn't forget, but I glossed over it :) For all purposes besides trait objects, my desugaring works and |
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
|
What about HRTB? Would this be viable? trait Bar<'a> { }
trait Foo = for<'a> Bar<'a>; |
This comment has been minimized.
This comment has been minimized.
|
@withoutboats From an implementation standpoint, it shouldn't be more difficult than other trait aliases. |
This comment has been minimized.
This comment has been minimized.
|
A concrete example for hiding HRTB came up in #rust: trait FromRef<T> = for<'a> From<&'a T>; |
This comment has been minimized.
This comment has been minimized.
|
Would Could the compiler auto-translate it to |
This comment has been minimized.
This comment has been minimized.
|
I don't know how implementing Here's impl<'a> From<&'a u32> for u32 {
fn from(x: &'a u32) -> u32 { *x }
}Note that the impl FromRef<u32> for u32 {
fn from(x: &u32) -> u32 { *x }
}The normal meaning of this would be to elide a lifetime parameter on the Special elisions for implementing HRTB trait aliases don't seem like a good idea to me (and AFAICT can't scale to an HRTB trait alias with multiple higher ranked lifetimes), but not being able to implement a trait alias transparently also seems like a problem. |
This comment has been minimized.
This comment has been minimized.
|
@withoutboats I don't see a problem with not being able to implement a trait alias if you can't implement what it aliases to to start with. It's not like you can |
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
|
@withoutboats that's already the case for trait inheritance. For example, you can't implement |
This comment has been minimized.
This comment has been minimized.
|
Sure, but in that case its relatively easy to learn what trait you need to implement. I'm concerned that using this feature to obscure HRTB will lead to a situation where a less expert Rust user sees that they need to implement I don't have a solution; HRTB just present a pretty serious accessibility problem in general. |
This comment has been minimized.
This comment has been minimized.
|
Current situation:
|
This comment has been minimized.
This comment has been minimized.
|
Is |
This comment has been minimized.
This comment has been minimized.
|
@durka The point is to hide the |
aturon
self-assigned this
Sep 29, 2016
This comment has been minimized.
This comment has been minimized.
|
I think I'm generally in favor of adding some kind of shorthand for "trait aliases" (or "where-clause aliases", depending on your POV). @aturon and I talked about it recently and we felt like the syntax ought to be modeled on the syntax for declaring a new trait with supertraits, but with trait SymPartialEq<T> = PartialEq<T> where T: PartialEq<Self>;then you could do: fn foo<T, U>() where T: SymPartialEq<U>and it would be equivalent to fn foo<T, U>() where T: PartialEq<U>, U: PartialEq<T>I'm not sure what to do about the case where you want just a where-clause though, and no Not sure if I love the precise syntax here, but there is something nice about the symmetry with supertrait clauses, and the "bias" to always have a It seems good for the more common cases that don't require where clauses, e.g. trait Foo = Bar + Baz; |
This comment has been minimized.
This comment has been minimized.
|
Maybe |
This comment has been minimized.
This comment has been minimized.
|
What would be the difference between On Wed, Nov 2, 2016 at 4:48 PM, Josh Stone notifications@github.com wrote:
|
This comment has been minimized.
This comment has been minimized.
bluss
commented
Nov 2, 2016
|
How are associated types handled? I commonly use things like |
phaazon
deleted the
phaazon:trait-alias
branch
Apr 25, 2017
This comment has been minimized.
This comment has been minimized.
|
Create a description of the process here: rust-lang/rust-forge#54 |
glyn
added a commit
to cloudfoundry/jvmkill
that referenced
this pull request
Jun 15, 2017
glyn
added a commit
to cloudfoundry/jvmkill
that referenced
this pull request
Jun 16, 2017
glyn
added a commit
to cloudfoundry/jvmkill
that referenced
this pull request
Jun 16, 2017
glyn
added a commit
to cloudfoundry/jvmkill
that referenced
this pull request
Jun 16, 2017
glyn
added a commit
to cloudfoundry/jvmkill
that referenced
this pull request
Jun 16, 2017
glyn
added a commit
to cloudfoundry/jvmkill
that referenced
this pull request
Jun 16, 2017
glyn
added a commit
to cloudfoundry/jvmkill
that referenced
this pull request
Jun 16, 2017
glyn
added a commit
to cloudfoundry/jvmkill
that referenced
this pull request
Jun 26, 2017
glyn
added a commit
to cloudfoundry/jvmkill
that referenced
this pull request
Jun 29, 2017
glyn
added a commit
to cloudfoundry/jvmkill
that referenced
this pull request
Jul 5, 2017
cramertj
referenced this pull request
Aug 30, 2017
Merged
Named existentials and impl Trait variable declarations #2071
withoutboats
referenced this pull request
Sep 2, 2017
Open
🔬 Tracking issue for generic associated types (GAT) #44265
This comment has been minimized.
This comment has been minimized.
|
Haven't read all the comments, but by the existing grammar is |
This comment has been minimized.
This comment has been minimized.
|
It's allowed by the implemented parser and I guess it'll mean the same as
`trait Trait {}`.
…On Tue, Feb 27, 2018 at 1:02 AM, Clar Roʒe ***@***.***> wrote:
Haven't read all the comments, but by the existing grammar is trait Trait
= ; allowed?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1733 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAC3n6PztcUX_tH3XNbrQXb_NLJIXejkks5tY5psgaJpZM4JxjCo>
.
|
This comment has been minimized.
This comment has been minimized.
|
I'd have guessed it would be a bound which accepted all types. If that works, I'd also expect |
This comment has been minimized.
This comment has been minimized.
|
That makes more sense, yes.
…On Tue, Feb 27, 2018 at 3:01 AM, Taylor Cramer ***@***.***> wrote:
Hm? I'd have guessed it would be a bound which accepted all types. If that
works, I'd also expect trait Static = 'static; to be a bound which
accepted all 'static types.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1733 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAC3n75MvU_6vy55rh8CKcnRB-xW5CJtks5tY7ZFgaJpZM4JxjCo>
.
|
This comment has been minimized.
This comment has been minimized.
Boscop
commented
Mar 26, 2018
•
|
Would this also work? trait Foo<'a> = Bar<'a> + 'a;It should! |
This comment has been minimized.
This comment has been minimized.
|
@Boscop The RFC enables it, yes. |
phaazon commentedAug 31, 2016
•
edited
Rendered
Trait alias would enable aliasing traits.
Useful especially when we want to lift the name from generic to specific (a bit like
Resulton a lot of modules, but for traits):