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 upEmpty struct with braces take 2 #218
Conversation
pnkfelix
added some commits
Aug 28, 2014
This comment has been minimized.
This comment has been minimized.
|
Thanks for taking the time to do this properly. |
pnkfelix
added some commits
Aug 29, 2014
This comment has been minimized.
This comment has been minimized.
gsingh93
commented
Aug 29, 2014
|
My preference is still to only have one way to do it, and that would be |
This comment has been minimized.
This comment has been minimized.
|
I'm still in favor of the status quo, but I am sympathetic to the macro expansion argument. However, if we're supporting |
This comment has been minimized.
This comment has been minimized.
SiegeLord
commented
Aug 31, 2014
|
+1 to "Always Require Braces". Alternatively, (not sure if this was proposed in the RFC), treat |
This comment has been minimized.
This comment has been minimized.
|
@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 |
alexcrichton
assigned
pnkfelix
Sep 4, 2014
alexcrichton
force-pushed the
rust-lang:master
branch
from
6357402
to
e0acdf4
Sep 11, 2014
This comment has been minimized.
This comment has been minimized.
|
+1 to "Always Require Braces". Try to "do a thing in one way". |
aturon
force-pushed the
rust-lang:master
branch
from
4c0bebf
to
b1d1bfd
Sep 16, 2014
This comment has been minimized.
This comment has been minimized.
|
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 |
This comment has been minimized.
This comment has been minimized.
|
@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. |
This comment has been minimized.
This comment has been minimized.
|
cc me |
This comment has been minimized.
This comment has been minimized.
I don't think it's a good smell supporting so many ways to do a very simple thing. |
This comment has been minimized.
This comment has been minimized.
|
@CloudiDust I cannot tell from your comment; are you overlooking the detail that the RFC explicitly explains why supporting |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
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.) |
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
|
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 |
This comment has been minimized.
This comment has been minimized.
|
I'm 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). |
This comment has been minimized.
This comment has been minimized.
|
@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 } } |
This comment has been minimized.
This comment has been minimized.
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):
|
This comment has been minimized.
This comment has been minimized.
|
@eddyb (I don't personally think that looks better, but it's neither here nor there.) |
This comment has been minimized.
This comment has been minimized.
|
@nikomatsakis I should have phrased that as "it would look more like a natural feature". |
This comment has been minimized.
This comment has been minimized.
jmesmon
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. |
This comment has been minimized.
This comment has been minimized.
|
@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). |
This comment has been minimized.
This comment has been minimized.
|
@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 |
This comment has been minimized.
This comment has been minimized.
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 |
This comment has been minimized.
This comment has been minimized.
|
@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. |
This comment has been minimized.
This comment has been minimized.
|
@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
assigned
nikomatsakis
and unassigned
pnkfelix
Apr 9, 2015
This comment has been minimized.
This comment has been minimized.
|
@pnkfelix never mind, having given this more thought, I think that your specification is compatible with what I want -- that is, |
nikomatsakis
referenced this pull request
Apr 10, 2015
Closed
Empty struct with braces tracking issue (RFC 218) #24266
nikomatsakis
merged commit f44ac71
into
rust-lang:master
Apr 10, 2015
This comment has been minimized.
This comment has been minimized.
|
RFC 218 is accepted. Tracking issue: rust-lang/rust#24266 |
pnkfelix commentedAug 29, 2014
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.