-
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
Require public trait items to be marked pub
#227
Conversation
Maybe I should have also mentioned that in an implementation of a trait for a concrete type, any definition for a public trait item must be marked |
(This would be improved with some code examples.) |
Sounds good to me. I'd like to see unification of traits and modules in Rusts 2.0 or something like that, and this pushes Rust ever so slightly in that direction. |
6357402
to
e0acdf4
Compare
Implementing this change also allows backwards compatible removing it again in the future in case private trait items are not wanted after all: Just make the |
There was some discussion of this RFC at tonight's team meeting: discussion I think the team is generally positive about going in this direction, but there were some questions about whether putting the |
As @pnkfelix said, the biggest sticking point was whether or not one ought to repeat the On the pro side of repeating
On the con side of repeating
Some things to consider:
Personally, I think the most likely semantics for private items in a trait would be that they can be defined anywhere but referenced only from within the module that defined the trait. I think I would prefer to do this by just denoting private things via the absence of pub, despite the possible confusion that arises, because having multiple ways to designate privacy feels like overkill -- instead people will just have to learn what privacy in an impl means. But I'm not sure about this yet. |
For motivation, read The D Programming Language chapter 6.9.1 The Non-Virtual Interface (NVI) Idiom (by the way, we also need |
@nikomatsakis when you write "I think I would prefer to do this by just denoting private things via the absence of pub", I interpret it as a statement in support of this RFC. Is that correct, or do I misunderstand you? I am a little wary of letting the discussion here get bogged down in discussion of the details of the semantics of privacy. . . but I do agree that a small example is warranted. And by that I mean something that is both 1. not stuck behind a paywall (hint hint), and 2. written in a hypothetical extension of Rust. |
Up front caveat: I normally would not use an in-progress RFC to provide motivation for an unrelated RFC. However, @nikomatsakis and i have been talking much about this other topic recently, and so I thought he might be able to provide much insight into whether this actually could be a use case in the long term (like Rust 2.0 ... or maybe Rust 1.1 ...). @nikomatsakis also, in case you are interested: I thought that private trait items might possibly be motivated by cases I encountered while working on the placement box RFC #470. In particular, it might be possible (and desirable) to make things like the particular impl of I am thinking of something like this: pub trait Placer {
pub type Data;
pub type Owner;
type Interim: PlacementAgent<Self::Data, Self::Owner>;
fn make_place(self) -> Interim;
} This illustrates that the input Like so:
(Of course this probably does not work in any way with today's expansion based prototype rust-lang/rust#18233 since the privacy pass is going to be looking at the expanded code.) |
I presented this use case (for more efficient for-loops) a while ago but it turned out to be void. The general idea of it might be useful in other contexts though. The example is written assuming public by default and marking privates by |
I'd personally prefer if we simply reserved (Also we may want |
Kind of frustrating that I have a better solution (syntactically and semantically), but am mostly incapable of writing RFCs. Also that there's zero chance it would be adopted, because 1.0. (Which are not unconnected.) |
@glaebhoerl could you elaborate a little on your idea? |
Sorry to hear that. Do you mean a solution to traits with private items? What would the backwards-incompatible changes be? |
There's a partially written RFC here. The part specifically about closed traits is nearer the end (but builds on ideas from the earlier parts). See also the private supertraits section here, which was the genesis of this here RFC, and this earlier rejected RFC which is essentially the same idea as in the first link. The idea of closed traits just falls out as the intersection of these two ideas. The basic idea is that In each case the difference is that with the abstract/closed flavors, the type author is "hiding something" - it is an abstract type, while in the data/open case, nothing is hidden - it is "just data". In one case, fields (methods) are private by default, in the other, everything is public. Only the owner of the type may construct (via a literal) and pattern match on an abstract type, anyone may do so for data types. Built-in traits are derived automatically for data types, and must be implemented explicitly for abstract types. With an abstract type, it should not be possible for clients to observe anything that is not intentionally exposed by the author. And so on. With the analogy of See the linked RFCs for a more thorough explanation. TL;DR Existing |
ping @pnkfelix, this seems to've fallen through the cracks? |
I think we should close this. There doesn't seem to be momentum and it's something we can address later without breaking backwards compatibility if we so choose. |
How could this be addressed later? By re-adding the |
Or we could add (cc also @freebroccolo) |
That is one possibility, yes. Or perhaps a modifier on the trait that makes the items private by default. -------- Original message -------- How could this be addressed later? By re-adding the priv keyword? — |
I agree we should close this. |
OK, it seems pretty clear that nothing on this front is going to happen before 1.0, and we'll just have to use one of the suggested "workarounds" if we want to add private trait items later on. In the interest of cleaning up the RFC PRs, I'm going to go ahead and close this. Thanks for the PR! |
View