-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Add export-like keyword for private names in a module #30204
Comments
We could also use |
Related: #12069 |
An alternative design is to require an explicit |
Another thought I've had is that "private globals" should probably be private to a given file. When you use |
That could similarly be accomplished with |
That was what I meant, but you're right that this would be kind of annoying for testing stuff. |
Having a way to access the private bindings would be Julian. In #12069 using |
Since making binding private has a cost of making things such as unit testing more difficult, I think it should not be the default option. However, I think if the user really, really wants to make a binding private (i.e. inaccessible outside the module) he should be able to do so by explicitly writing |
If you really want to make something private, you already can using julia> module M
let x = 1
global f() = x
end
end
Main.M
julia> M.f()
1
julia> M.x
ERROR: UndefVarError: x not defined |
A very simple approach could be to have a nested module:
In order to increase convenience, one would probably want some macro
Re really preventing access: I think this is a very bad idea. We should serve the library user, not the library author. The concrete desires of the caller override the imagination of the callee, at least unless we want to perform optimizations that rely on "hidden internals" being inaccessible (like e.g. |
Currently, we don't have a way to make names private. The convention is to prepend
_
to the name. But this is just a convention, which users can and do ignore.Omitting how and why the inability to make names private can ruin everything in large codebases, I propose to add
private
keyword for making names private that is analogous toexport
. So something, which currently written aswould be written as
and
c
andd
would not be accessible outside the module. This also makes the code more clean and readable (without hairy names starting with_
), which is something that Julia generally strives for. This way we would have 3 kinds of names:export
ed)I have no knowledge of compiler, but I would expect that this can also enable some compiler optimizations that otherwise are not possible.
The text was updated successfully, but these errors were encountered: