Allow importing of just methods from a module
Summary
Inko should allow developers to import specific methods from a module, instead of only being able to import an entire module or constants defined in said module. This would allow developers to write code such as the following:
import std::parse::(digit, char)
digit + digit.or(char('_')).zero_or_more
Instead of this:
import std::parse
parse.digit + parse.digit.or(char('_')).zero_or_more
Motivation
Importing methods was not allowed as the compiler was not able to determine
what object to use as the received, due to self
being an explicit argument.
Since commit 79103c8e self
is no longer an
argument, removing this limitation.
Giving developers the ability to import individual methods can make code less verbose, especially when using multiple methods from a module many times (see the above example).
Implementation
Only compiler changes are necessary to make this work, and from the top of my head they should be fairly straightforward. The lookup order would be changed to the following:
- Local variables
- Methods defined in
self
- Methods defined in the module that defines the running code
- Methods imported into the module that defines the running code
- Variables imported into the module that defines the running code
Drawbacks
Some compiler changes are necessary, and in some cases it may not be clear where a method is coming from.
Related work
- Python
- Rust supports this using
use module_name::function_name