Private constants and methods
Summary
Constants, types, and methods are currently all public, allowing anybody to use them anywhere. In certain cases you may wish to make certain constants or methods inaccessible. Inko should support marking constants (defined using let
) and methods as private, by prefixing their names with an underscore. Types (objects and traits) are always public, as otherwise a private type could be leaked through a public type.
Motivation
While implementing std::net
, and std::net::ip
in particular, certain code had to be extracted into a method to make it easier to reuse the logic. This logic however is an implementation detail, and serves no real purpose outside of the module. Due to everything being public, these methods would be part of the public API. Removing or changing the implementation such that these methods are no longer necessary would thus require a major version increase.
Prefixing names with an underscore removes the need for additional keywords. This means we don't have to update the lexer, parser, and syntax highlighting rules to support these new keywords. Another benefit is that when using the constant or method, it immediately becomes clear it is private; removing the need for jumping to the definition to find out.
Implementation
The visibility is only enforced at compile time, as doing this during runtime adds unnecessary overhead. When importing a constant, we disallow this if it starts with a _
. Types defined using object
and trait
can not start with a _
, to prevent leaking of private types through public ones. When sending a message that starts with a _
, we require that the underlying method is defined in the same module as the call site.
Drawbacks
This requires some additional compiler complexity and documentation changes.
Related work
Python's "Private Variables" feature is the most similar.