Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This adds private types to the language. #2950 had an RFC for it, but this PR defines the final semantics, which I'll explain now.
Basic example:
We can start using private types where we previously could only use
:nodoc:
to only hide them from docs, but they could still be found and used with code. Some places where we could use them: Hash::Entry, the many iterator types, some constants in Base64, and many more places.One can apply
private
to all of these: class, module, lib, enum, alias, and constants too:Like methods, private types can be seen in subclasses. For example:
Finally, private types can be marked at the top level: this makes them file-private, just like what using
private
with methods and macros at the top-level does. For example:This feature is probably only useful to define types in specs that we don't want to pollute or conflict in the global namespace. For example we could use them here, here, here and many other specs.
I can understand that this last feature (file-private types) might not seem like needed, or strange, but the reason we introduced file-private methods in the first place was to define helper functions in specs without polluting the global namespace, and at least for me they have worked pretty well. I also sometimes use them in the compiler where I just want to define pure functions that don't really belong to any type, but I only need them in that file. For example here.
Private types can of course be returned from methods and used perfectly fine outside the namespace where they are defined.
private
only makes it impossible to reference them by name outside the namespace. For example this is valid:One can't use
protected
with types. We could maybe think about it later, but I think justprivate
is enough (for example Ruby hasprivate_constant
but noprotected_constant
).