-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Empty struct with braces take 2 #218
Empty struct with braces take 2 #218
Conversation
…atching their heads.
Thanks for taking the time to do this properly. |
Figure I should at least let those commenters get a bucket in the summary, even though I personally am ambivalent at best about "+1" comments.
My preference is still to only have one way to do it, and that would be |
I'm still in favor of the status quo, but I am sympathetic to the macro expansion argument. However, if we're supporting |
+1 to "Always Require Braces". Alternatively, (not sure if this was proposed in the RFC), treat |
@SiegeLord hmm, I guess I regarded that option as a particular variant of "Always Require Braces", since you can then encode But I guess I should put it in as an explicit sub-variation, in terms of continuing to support the notation |
6357402
to
e0acdf4
Compare
+1 to "Always Require Braces". Try to "do a thing in one way". |
I think we should either maintain the status quo, or support all three ways to do this thing:
Because choosing either And if we are to support those two, why don't we continue to support Also, if we support both So I think we should still prefer to use |
@liigo, structs already support doing one thing in two ways: you can choose whether to leave out the last comma or not.
So I think having three ways to declare an empty struct is okay. |
cc me |
I don't think it's a good smell supporting so many ways to do a very simple thing. |
@CloudiDust I cannot tell from your comment; are you overlooking the detail that the RFC explicitly explains why supporting |
At a recent meeting it was pointed out that the approach of this RFC is a little too simplistic, in that it would break the current property that when you write So I plan to address this by modifying this proposal slightly: instead of treating (Arguably the latter proposal is sufficiently complicated as to push me further toward the "Always Require Braces" camp. But I have not landed there yet.) |
Is there a technical reason that flexible structs can't be constructed and matched with braces? With that restriction the 'flexiness' of the struct matters to the caller, though it otherwise seems irrelevant. On cursory thought my preference is to make the non-brace form a special allowance for declaration, construction and matching for any struct that doesn't have fields. |
👍 for allowing If On the other hand, I've always found it jarring having to put braces after impls of marker traits. I'd much rather like to see |
I'm 👍 on the RFC as written, I think. I like the idea that the canonical way to declare a struct is with braces, but we offer shorthands analogous to those available to enum variants. In this way, struct S { }
const S = S { }; Similarly, struct S { 0: T }
const fn S(t: T) { S { 0: T } } With the caveat that you can't declare fields with numeric names and that we haven't accepted #911, of course. Still, that makes sense to me (and the same, incidentally, applies to enum variants). |
@nikomatsakis If we had adopted a different struct literal style, your second example would look better: const fn S(t: T) -> S { S { .0 = t } } |
I don't understand this question... as I read it, the RFC seemed to say that a flexible struct could be constructed and matched with braces (or without):
|
@eddyb (I don't personally think that looks better, but it's neither here nor there.) |
@nikomatsakis I should have phrased that as "it would look more like a natural feature". |
I've run into the similar problem to this with enum variants when trying to wrap & generate enums within a macro:
My conclusion after a bit of macro contortion is that due to this syntax requirement, macro_rules! can't presently wrap enums that include no-arg variants :( I expect macros-wrapping struct definitions run into the same problem, but I haven't looked down that path yet. |
@jmesmon I've done it a few times in the past, but it complicates the macro significantly (sometimes the end result is so bad it's just not worth it). |
@pnkfelix something I was just wondering. Today, tuple-structs can't be constructed using brace syntax, so is there a reason to have "flexible" structs support both options? It seems mildly inconsistent? Or am I confused? (Personally though I think I'd rather that all structs can be built using braces, just using |
I think I stated my concern backwards: this is from the RFC "Both braced and flexible empty structs can be constructed via the expression syntax S { } ("new"). Flexible empty structs, as today, can also be constructed via the expression syntax S." It seems to indicate that braced structs can't be constructed with the non-braced syntax - the case it doesn't mention is 'braced structs can be constructed via the expression syntax |
@brson okay. that's right, braced structs deliberately cannot be constructed via non-brace syntax, because braced structs do not have an entry in the value namespace, only the type namespace. |
@nikomatsakis If I understand you correctly, It sounds like you are asking "why did the RFC take this route, and not go down the route of the 'Never synonymous' option?" is that right? I don't really have more to add on the topic than what I wrote there: its hard to justify having both syntaxes available if you're not actually going to be flexible about their usage. Or maybe you are semi-implicitly asking for a new alternative to be added to the RFC that attempts to pull tuple-structs into the mix? I am not personally opposed to a (I argue its orthogonal because, to my knowledge, there is no empty tuple struct, but all of the structs addressed by this RFC are empty.) |
@pnkfelix never mind, having given this more thought, I think that your specification is compatible with what I want -- that is, |
RFC 218 is accepted. Tracking issue: rust-lang/rust#24266 |
When a struct type S has no fields (a so-called "empty struct"), allow it to be defined via either struct S; or struct S {}. Allow instances of
struct S
to be constructed and pattern-matched via either S or S {}. Allow instances ofstruct S {}
to be constructed and pattern-matched viaS {}
.Rendered
This is a semi-revival of RFC PR #147, but hopefully well-fleshed out enough for proper consideration.