Optional namespaces with shared aliases #448
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
For design, see #434
This PR adds optional namespaces to Dyon as a way to organize code and avoid name collisions. At the moment this is only supported for loaded modules, not external functions.
How dynamic modules work in Dyon
Dyon uses dynamic modules to organize code. In order for one module to depend on another, you write a loader script, like this:
This puts the functions declared in
graphics
in the prelude of the moduleprogram
. You do not have to write any imports, you just call them:This design makes it easy to refactor code, because you can simply copy and paste a function to another module and expect it to work. Since module loading is controlled by the loader script, you can update parts of the program while running.
How optional namespaces work
An optional namespace can be declared at the beginning of the file with the
ns
keyword:This puts all functions in this file in an extra namespace
graphics
.You can also uses nested namespaces, like this:
To use a namespace, you write
use
after the line withns
and give it an alias:All
use
statements requires an alias. This is designed to make refactoring easier, because you can just change the top of the file instead of going through the whole code.You can also share an alias between multiple imports, like this:
Notice that you still can use the default namespace, so without any name collisions you can just write like this:
Import specific functions while excluding others
When importing specific functions, they will shadow any function in global import:
Rename a specific function