Skip to content

GitLab

  • Projects
  • Groups
  • Snippets
  • Help
    • Loading...
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
    • Switch to GitLab Next
  • Sign in / Register
inko
inko
  • Project overview
    • Project overview
    • Details
    • Activity
    • Releases
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Locked Files
  • Issues 43
    • Issues 43
    • List
    • Boards
    • Labels
    • Service Desk
    • Milestones
    • Iterations
  • Merge Requests 1
    • Merge Requests 1
  • CI / CD
    • CI / CD
    • Pipelines
    • Jobs
    • Schedules
    • Test Cases
  • Security & Compliance
    • Security & Compliance
    • Dependency List
    • License Compliance
  • Operations
    • Operations
    • Incidents
    • Environments
  • Analytics
    • Analytics
    • CI / CD
    • Code Review
    • Insights
    • Issue
    • Repository
    • Value Stream
  • Members
    • Members
  • Collapse sidebar
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Inko
  • inkoinko
  • Issues
  • #162

Closed
Open
Opened Apr 26, 2019 by Yorick Peterse@yorickpeterseOwner

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.

Assignee
Assign to
Backlog
Milestone
Backlog
Assign milestone
Time tracking
None
Due date
None
Reference: inko-lang/inko#162