Uh oh!
There was an error while loading. Please reload this page.
Empty struct with braces take 2 - #218
Conversation
…atching their heads.
mahkoh
commented
Aug 29, 2014
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.
gsingh93
commented
Aug 29, 2014
My preference is still to only have one way to do it, and that would be |
lilyball
commented
Aug 29, 2014
I'm still in favor of the status quo, but I am sympathetic to the macro expansion argument. However, if we're supporting |
SiegeLord
commented
Aug 31, 2014
+1 to "Always Require Braces". Alternatively, (not sure if this was proposed in the RFC), treat |
pnkfelix
commented
Aug 31, 2014
@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 |
liigo
commented
Sep 13, 2014
+1 to "Always Require Braces". Try to "do a thing in one way". |
CloudiDust
commented
Sep 19, 2014
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 |
CloudiDust
commented
Sep 19, 2014
@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. |
nikomatsakis
commented
Sep 19, 2014
cc me |
liigo
commented
Sep 25, 2014
I don't think it's a good smell supporting so many ways to do a very simple thing. |
pnkfelix
commented
Sep 25, 2014
@CloudiDust I cannot tell from your comment; are you overlooking the detail that the RFC explicitly explains why supporting |
CloudiDust
commented
Sep 25, 2014
pnkfelix
commented
Oct 7, 2014
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.) |
brson
commented
Feb 26, 2015
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. |
vadimcn
commented
Feb 26, 2015
👍 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 |
nikomatsakis
commented
Feb 27, 2015
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, structS{}constS = S{};Similarly, structS{0:T}constfnS(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). |
eddyb
commented
Feb 27, 2015
@nikomatsakis If we had adopted a different struct literal style, your second example would look better: constfnS(t:T) -> S{S{.0 = t }} |
nikomatsakis
commented
Feb 27, 2015
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):
|
nikomatsakis
commented
Feb 27, 2015
@eddyb (I don't personally think that looks better, but it's neither here nor there.) |
eddyb
commented
Feb 27, 2015
@nikomatsakis I should have phrased that as "it would look more like a natural feature". |
codyps
commented
Feb 27, 2015
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. |
eddyb
commented
Feb 28, 2015
@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). |
nikomatsakis
commented
Mar 11, 2015
@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 |
brson
commented
Mar 11, 2015
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 |
pnkfelix
commented
Mar 11, 2015
@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. |
pnkfelix
commented
Mar 11, 2015
@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.) |
nikomatsakis
commented
Apr 10, 2015
@pnkfelix never mind, having given this more thought, I think that your specification is compatible with what I want -- that is, |
nikomatsakis
commented
Apr 10, 2015
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 Sto 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.