Skip to content

Add export-like keyword for private names in a module #30204

@AzamatB

Description

@AzamatB

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 to export. So something, which currently written as

module Mod

export a, b

a(x) = _a(x)
b(x) = _b(x)

_a(x) = ... # internal implementation
_b(x) = ... # internal implementation
end

would be written as

module Mod

export a, b
private c, d

a(x) = c(x)
b(x) = d(x)

c(x) = ... # internal implementation
d(x) = ... # internal implementation
end

and c and d 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:

  • truly public names (i.e. exported)
  • public names that are accessible with module name (default option)
  • private names that are not accessible outside the module

I have no knowledge of compiler, but I would expect that this can also enable some compiler optimizations that otherwise are not possible.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions