-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Removing priv
as a keyword
#8122
Comments
Well, Haskell exposes members explicitly, as does Node. Java asks for I think there's a good case, not only in language precedent but as a On Tue, Jul 30, 2013 at 4:06 AM, Alex Crichton [email protected]:
Andrés Osinski |
I don't think it's possible to pattern match on a struct that has one or more private members. Having to mark almost every struct field as |
I agree that "private by default, always" is a simpler rule, but I caution against overexuberance at the thought of removing a keyword. It very well might not be worth the loss of convenience. One good data point would be to figure out how many of the currently-public fields in the compiler could actually be private. An off-by-default lint pass might help with this. |
I was almost sold on this until I realized this meant you would have to write, for example, |
Can we change |
@bblum: I would be ok with @blake2-ppc: I would certainly hope so! |
I guess I'm in somewhat in favor since I think pub struct fields are the wrong default. I suspect that inheriting visibility from enums to their variants will not work out satisfactorily though - we tried that for impls and found it confusing. |
@brson, although isn't that what enums do right now? (inheriting visibility from the type name to the variants). |
I don't think private-by-default struct fields are a good thing. You cannot construct them at all without constructor functions, which just makes it more work. |
The fields would be private by-default only, we wouldn't require any more constructor functions than we have now. |
Well, yes, but it also requires an explosion of Then again, traits already require On Wed, Jul 31, 2013 at 6:12 PM, Daniel Micay [email protected]:
|
I think private-by-default is better because a wrongly private field is going to be quickly noticed. I wouldn't be surprised if we actually had more structs where the fields should be private, than those used as transparent blobs of data. It's also odd for the fields in newtypes and tuple structs to be public by-default (or only public, as they are right now). You would expect half of their purpose to be concealing an implementation detail, otherwise you would just use a type alias (unless you needed to override a trait implementation). |
This is true. After thinking about it some more I'm fine with this. |
Nominating for the backwards-compatible milestone. |
accepted for backwards-compatible (note that we haven't decided for-sure to do this, but making the decision is on milestone 2) |
Regardless of the fate of the |
+1 On Wed, Oct 30, 2013 at 9:03 AM, Ben Striegel [email protected]:
|
I believe that the main case holding back making fields private-by-default is the simple:
That's particularly easy to read, and if you compare it to
It's a little more verbose. This is an obvious consequence of private-by-default fields, but it's also unfortunate to regress in this simple example. We haven't decided yet whether this is an acceptable regression. Some attempts were made to make this syntax concise as well, and most of them focused on changing the declaration of the struct to say "I want my fields public by default". In this case, a normal We didn't really reach any good conclusions in this case, and as a result this sort of stagnated and stopped making progress. This is high up on my agenda of things to bring up at meetings, however, and I think that I'll get around to this at the next one. |
Has anyone suggested adopting private-by-default fields but also adding extending the syntax for struct/enum with a leading ((Update: I guess a suggestion like this is what @alexcrichton was alluding to at the end of his second to last paragraph of the previous comment.)) So to express (under priv-by-default rules): pub struct Point { pub x: int, pub y: int }
pub struct CP { pub x: int, pub y: int, col: Color } you could instead do pub struct Point { pub: x: int, y: int }
pub struct CP { pub: x: int, y: int, priv col: Color } This way, you don't pollute each field with a |
That's a nice solution. 👍 On Fri, Nov 15, 2013 at 3:40 AM, Felix S Klock II
|
I think I'm ok with @pnkfelix' solution. Removing a keyword is always nice. |
I think you could also use similar syntax to control the visibility of tuple structs innards (which is an annoying limitation right now). Then if you write
it would be an abstract type when viewed from outside the module (you couldn't access the uint). You would have to write:
to make a type whose tuple-ness is externally available. |
I checked with the folks in #haskell to make sure, and I don't think there's any use case for having a mixture of public and private variants. For constructing the type, which is usually what you care about, you can just use functions, so the question hinges on pattern matching. Does anyone else see a reason why you might want to let clients check for particular variants via pattern matching but not others (and therefore be required to use If that's so, then I think there's three inter-related questions left:
|
IMO, variants shouldn't have privacy and should be imported into a scope when the type is imported. This would remove the need for the complexity of an |
Explicit is better than implicit. If you leave a variable without any keyword for visibility ... was that because you meant In Java, you have the case of "package protected", which is the default and does not have any keyword for it. I rarely use this visibility: in general I make everything private. But, in the rare circumstances i use "package protected"... I put an annotation I've created specially for this situation, meaning: Yes! I know exactly what I'm doing. I even an additional compilation rule which detect the situation of "package protected" variables which are not annotated properly, which means: yes... I forgot to review this code. |
Taking Scala as an example, the default is that classes and class members are all public by default, making it a lot less verbose than Java in this aspect. |
IMHO, reducing verbosity is a good thing in general. I just don't think that In Java, "package protected" variables do not have an explicit keyword
In my code, I usually put an annotation created specially for such sort Thanks Richard Gomes On 06/03/14 17:56, Ziad Hatahet wrote:
|
2014年3月7日 上午2:19于 "Richard Gomes" [email protected]写道:
Agree.
|
I just came up with a use-case for private enum variants. Specifically, I have a type that needs to be public, but its implementation needs to be private, and the only sane implementation is as an enum. If |
I've used enums with all private variants as well, but am not really opposed to wrapping in a struct. |
@kballard I would like to have that ability as well. (What I don't want to have, but we currently do, is the option to have some variants public, and others private. I don't think that makes any sense.) (See also some related thoughts from the ML.) Edit: To summarize, I would like to support:
What I don't want to support:
I'm mostly indifferent as to whether we support:
This makes thinking about syntax a bit easier, but there's still no obvious winner. |
This is a necessary change in preparation for switching the defaults as part of rust-lang#8122. RFC: 0004-private-fields
This change is in preparation for rust-lang#8122. Nothing is currently done with these visibility qualifiers, they are just parsed and accepted by the compiler. RFC: 0004-private-fields
This commit switches privacy's checking of fields to have *all* fields be private by default. This does not yet change tuple structs, this only affects structs with named fields. The fallout of this change will follow shortly. RFC: 0004-private-fields cc rust-lang#8122 Closes rust-lang#11809
see also #13535 |
…lexcrichton Hi! While researching stuff for the reference and the grammar, I came across a few mentions of using the `priv` keyword that was removed in 0.11.0 (rust-lang#13547, rust-lang#8122, rust-lang/rfcs#26, [RFC 0026](https://github.com/rust-lang/rfcs/blob/master/text/0026-remove-priv.md)). One occurrence is a mention in the reference, a few are in comments, and a few are marking test functions. I left the test that makes sure you can't name an ident `priv` since it's still a reserved keyword. I did a little grepping around for `priv `, priv in backticks, `Private` etc and I think the remaining instances are fine, but if anyone knows anywhere in particular I should check for any other lingering mentions of `priv`, please let me know and I would be happy to! 🍂 🌊
…lexcrichton Hi! While researching stuff for the reference and the grammar, I came across a few mentions of using the `priv` keyword that was removed in 0.11.0 (rust-lang#13547, rust-lang#8122, rust-lang/rfcs#26, [RFC 0026](https://github.com/rust-lang/rfcs/blob/master/text/0026-remove-priv.md)). One occurrence is a mention in the reference, a few are in comments, and a few are marking test functions. I left the test that makes sure you can't name an ident `priv` since it's still a reserved keyword. I did a little grepping around for `priv `, priv in backticks, `Private` etc and I think the remaining instances are fine, but if anyone knows anywhere in particular I should check for any other lingering mentions of `priv`, please let me know and I would be happy to! 🍂 🌊
Now that you can't really put
pub
at the top-level of things like impl/extern/trait, you don't need thepriv
keyword to control method visibility of various items because they're all private by default.pros:
difficulties:
priv
I'd vote to remove it.cons:
priv
, and if we removed priv there would be no way to make half the enum variants public and half private. In theorypub enum
makes everything public and you could otherwise writepub
on each variant, but the type name would still then have to be public and if it didn't havepub
in front of it it wouldn't really make senseWhat do others think about this?
The text was updated successfully, but these errors were encountered: