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 upType alias enum variants #2338
Conversation
scottmcm
added
the
T-lang
label
Feb 15, 2018
This comment has been minimized.
This comment has been minimized.
|
I have run into this in my projects several times. Fortunately, I have access to the original type definition, so I could add a "constructor" from variant to work around this. The absence of this functionality came as a surprise to me, although it is not a bug in a sense. Implementing this would reduce the frustration people experience when initially they implicitly thought it would work, but later found out it would not. Another question is how close this relates to using alias to construct unit and tuple. From a usage point of view, they are the same surprising unsupported functionality of alias. Do they differ a lot inside the compiler? Should we discuss both or it requires another RFC? At last, how much inconsistency would an implementation bring to the compiler if we must introduce them? |
This comment has been minimized.
This comment has been minimized.
|
@WiSaGaN thanks for the feedback. I don't know much about the compiler internals, but I would guess the aliasing of unit and tuple types would not be very tied up in the enum variant handling, so I think it would make more sense to handle that in a different RFC. It would make sense to me, though, for much of the same reasons that I wrote up in this RFC. |
This comment has been minimized.
This comment has been minimized.
|
Tracking issue for this feature - rust-lang/rust#26264. |
This comment has been minimized.
This comment has been minimized.
|
The previous (very much reasonable) decision in rust-lang/rust#26264 was to resolve variants exactly like inherent associated items - variants themselves as associated types and their constructors as associated values.
For variants that would mean
|
This comment has been minimized.
This comment has been minimized.
|
I agree with @petrochenkov that
In the end, I think the change to move arguments from the variant to the type warrants its own discussion, since it a lot of code already out there, whereas this RFC restricts itself to slightly extending the allowed syntax to make aliases more useful. To maintain consistency (which I think are important to the ergonomics of aliases in general) the same rules should IMO apply to aliased and non-aliased enums. |
This comment has been minimized.
This comment has been minimized.
|
cc #2218 ("Generics: Make enum variant syntax consistent with other types") |
This comment has been minimized.
This comment has been minimized.
|
This would help Stylo a lot, which has a ton of type aliases to enums, and we need to import both the type alias and the enum it is based on if we want to build values of that type. |
aturon
self-assigned this
Feb 28, 2018
aturon
added
the
I-nominated
label
Feb 28, 2018
This comment has been minimized.
This comment has been minimized.
|
Nominated for discussion in a lang team meeting. |
withoutboats
removed
the
I-nominated
label
Mar 5, 2018
This comment has been minimized.
This comment has been minimized.
|
Removing I-nominated. I recall that we were generally in support of this feature, I think @nikomatsakis or @cramertj had more comments. |
This comment has been minimized.
This comment has been minimized.
|
The meeting agenda/notes mentioned this:
|
petrochenkov
referenced this pull request
Mar 5, 2018
Open
Enum variants completely shadow inherent associated functions. #48758
This comment has been minimized.
This comment has been minimized.
|
We discussed this RFC in the lang team meeting, and would like to move it into formal review. It has addressed the open questions that prevented us from previously accepting a PR on this topic. @rfcbot fcp merge |
rfcbot
added
the
proposed-final-comment-period
label
Mar 9, 2018
This comment has been minimized.
This comment has been minimized.
rfcbot
commented
Mar 9, 2018
•
|
Team member @aturon has proposed to merge this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once a majority of reviewers approve (and none object), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
This comment has been minimized.
This comment has been minimized.
|
@rfcbot concern #2338 (comment) |
This comment has been minimized.
This comment has been minimized.
|
@nikomatsakis since a majority of the reviewers has approved now, can this enter FCP? |
This comment has been minimized.
This comment has been minimized.
rfcbot
commented
Mar 26, 2018
|
|
rfcbot
added
final-comment-period
and removed
proposed-final-comment-period
labels
Mar 26, 2018
This comment has been minimized.
This comment has been minimized.
warlord500
commented
Mar 29, 2018
•
|
if this rfc, were to be added to the compiler the following piece of code should compile correct? enum Bar<T> {
Test(i32),
Data(T),
}
type Foo<T> = Bar<T>;
fn main() {
let _may = Foo::Test::<i32>(1);
}I do think it is weird were we put the type annotation. i feel like it should be closer to the type like Vec::<i32>::new()It seems inconsistent to me. I am safe in assuming that there will be no more additional enum type annotation syntax? |
This comment has been minimized.
This comment has been minimized.
|
@warlord500 as explained quite clearly in the reference-level explanation, |
This comment has been minimized.
This comment has been minimized.
warlord500
commented
Apr 1, 2018
•
|
I am gonna apologize here! some of this comment, may come off as arrogant! also for possible any additional confusion this might add. Writing is not my strongest skill. I am gonna start off with, just because you find something clear doesn't In fact the very reason, I asked my question was to make sure I completely understood the RFC. As, I understand the RFC as now, this doesn't change grammar but does expand the possible interpretations of foo::Test::(1); access foo which can be module or enum or struct, if my understanding of rust the language is wrong please correct me. It is never a bad time to learn. If the first question's answer is no, then this RFC needs to be made clear in its entirety. thank you, for reading my extremely long rant! |
This comment has been minimized.
This comment has been minimized.
|
This RFC is indeed not intended to change the syntax. It just extends the semantics of the language so that referring to variants into aliased enums is now legal. I hope that clarifies things for you. |
This comment has been minimized.
This comment has been minimized.
warlord500
commented
Apr 1, 2018
|
thank you for short and sweet answer to my questions. @djc |
This comment has been minimized.
This comment has been minimized.
rfcbot
commented
Apr 5, 2018
|
The final comment period is now complete. |
Centril
referenced this pull request
Apr 5, 2018
Open
Tracking issue for RFC 2338, "Type alias enum variants" #49683
Centril
merged commit 1c851e0
into
rust-lang:master
Apr 5, 2018
This comment has been minimized.
This comment has been minimized.
|
Huzzah! This RFC has been merged! Tracking issue: rust-lang/rust#49683 |
alexreg
referenced this pull request
May 9, 2018
Closed
Allow enum variants to be used through a type alias of the enum #31179
ExpHP
referenced this pull request
Jul 6, 2018
Open
Compiler error message could be clearer: did you mean `variant::Init`? #52118
Centril
added
A-paths
A-typesystem
A-resolve
labels
Nov 23, 2018
alexreg
reviewed
Nov 26, 2018
| type Alias<T> = Option<T>; | ||
| mod foo { | ||
| pub use Alias::Some; |
This comment has been minimized.
This comment has been minimized.
alexreg
Nov 26, 2018
I'm inclined to think this shouldn't be allowed, or at least it shouldn't be part of the initial implementation. After all, even
pub use Option::Some;does not work right now, although
pub use ::std::option::Option::Some;does, however.
For me, the semantics are unclear, and I'd be inclined not to accept it by default.
Anyway, would appreciate the thoughts of @rust-lang/lang on this matter.
This comment has been minimized.
This comment has been minimized.
petrochenkov
Nov 26, 2018
Contributor
Yes, this can't work right now.
Option::Some is module-relative (enums and traits are kind of modules from name resolution's point of view), so they can be used in imports.
Alias::Some is type-relative, and we need type checking to determine what Alias actually refers to, so it cannot be used in imports due to phase separation.
djc commentedFeb 15, 2018
•
edited by Centril
This RFC proposes to allow access to enum variants through type aliases. This enables better abstraction/information hiding by encapsulating enums in aliases without having to create another enum type and requiring the conversion from and into the "alias" enum.