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 upRFC: Private fields by default #1
Conversation
This comment has been minimized.
This comment has been minimized.
|
I'm not sure that the behavior of mixed-privacy tuple structs makes a ton of sense. Mixed-privacy structural structs (is that really the term?) are pretty straightforward since they're inherently unordered, so patterns can ignore the existence of the private fields entirely: pub struct Foo {
a: int,
pub b: int,
c: int,
pub d: int,
}
let Foo { ref b, ref d, _ } = foo;It seems like that in the case of tuple structs, the existence and location of a private field has to be part of the public API, which seems like it defeats a lot of the purpose of having private fields: pub struct Foo(int, pub int, int, pub int);
let Foo(_, ref b, _, ref d) = f;Is there a use case for mixed-privacy tuple structs? One option could be to force all parameters to have the same visibility, but that seems like it would be a bit inconsistent. |
This comment has been minimized.
This comment has been minimized.
|
That's an interesting point. I would guess that as with structs, most tuple structs will have all-private fields. I would imagine that any all-public tuple structs would benefit from the same syntax of all-public structural structs (yeah, I just kinda made up that term). This would benefit from the planned pub struct Foo(pub int, int, char);
let Foo(first_field, ..) = ...;In terms of API stability, I think that I will amend this RFC to forbid the following: pub struct Foo(int);
let Foo(_) = ...;If this were allowed, then you couldn't add fields backwards compatibly. It would only deny pattern matches if all fields were private, however. |
This comment has been minimized.
This comment has been minimized.
|
Should the compiler also prevent public fields from appearing after private fields? |
This comment has been minimized.
This comment has been minimized.
|
I wouldn't expect the compiler to prevent an occurrence such as that. Perhaps some external tool which has extra lints targeted at API stability (not that there's much other than this), but it seems like a valid enough thing to do as part of the language (if a bit silly) |
brson
referenced
this pull request
in brson/rfcs
Mar 13, 2014
This comment has been minimized.
This comment has been minimized.
jfager
commented on active/0000-private-fields.md in 98565f7
Mar 15, 2014
|
For Baz, does this mean |
This comment has been minimized.
This comment has been minimized.
|
The latter, you'd have to explicitly ignore the private fields. |
This comment has been minimized.
This comment has been minimized.
jfager
commented on active/0000-private-fields.md in 98565f7
Mar 15, 2014
|
What does this look like for creating Baz? |
This comment has been minimized.
This comment has been minimized.
|
The struct cannot be created outside of the containing module (similarly to structural private fields) |
This comment has been minimized.
This comment has been minimized.
bill-myers
commented
Mar 16, 2014
|
An option could to be imitate C++, and have both a "struct" and "class" keywords with different privacy. My suggestion would be to make "struct" always all-public, make "class" private-by-default, and make a class with all public fields be an error, so that there is exactly one way to express any structure. It's not clear whether the added complexity of two keywords is worth not having to specify "pub" for all-public fields, but on the other hand C++ programmers are already familiar with that. In C#, struct and class have a different meaning (in Rust parlance, C# class is an implicit "type Foo = @mut Foo", while C# struct is a Rust Pod struct), so this might generate some confusion for C#-only programmers. On the other hand, additional syntax sugar might be tied into this, like automatically #deriving stuff for structs but not classes. Scala is another language with two keywords with different privacy and deriving semantics, which are called "class" and "case class". |
This comment has been minimized.
This comment has been minimized.
|
@bill-myers You might be interested in #10 wherein I take a similar tack, albeit with different syntax. |
glaebhoerl
referenced
this pull request
in glaebhoerl/rfcs
Mar 17, 2014
brson
merged commit 98565f7
into
rust-lang:master
Mar 25, 2014
This comment has been minimized.
This comment has been minimized.
brson
referenced this pull request
Mar 25, 2014
Closed
RFC: `abstract struct` and `abstract enum` #10
This comment has been minimized.
This comment has been minimized.
|
an easy method to make struct fields all public:
|
alexcrichton commentedMar 12, 2014
No description provided.