You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Dyon has optional namespaces to organize code and avoid name collisions.
One declared namespace per file (e.g. ns math::algebra::topology)
Followed by use statements (e.g. use math::algebra::topology as m)
Shared namespace aliases (see below)
An optional namespace does not take away functions in the default namespace, but adds it to a new namespace that you can use when needed. This means you do not have to import functions from a namespace to use them. You can use the default namespace as usual. The optional namespace is used when there is a risk of naming collision.
To declare a namespace, use the syntax ns <namespace>. You can only have one namespace in the same file:
ns math
// `add` is now in the namespace `math`fnadd(a:f64,b:f64) -> f64{ ...}
The namespace can be nested, e.g. ns math::algebra::topology. It should be unique to avoid naming collision with other modules.
To call a function using a namespace, you must add a use statement at the top, after the ns line. The use statement requires an alias, e.g. use math as m. This alias is also required when calling, to avoid ambiguity with the default namespace:
ns program::example::test
use math as m
fnmain(){
a := 1
b := 2
m::add(a, b)// using `math::add`add(a, b)// using `add` from default namespace}
You can import specific functions, leaving out the others. Specific imports are put inside curly braces, e.g. use math::{add, sub} as m:
use math::{add} as m
fnmain(){
a := 1
b := 2
m::add(a, b)// prefix is still required.}
You can rename specific functions:
use math::{add as addition} as m
fnmain(){
a := 1
b := 2
m::addition(a, b)}
Shared namespace aliases
You can use the same namespace alias for more than one import:
use math as m
use graphics as m
fnmain(){
a := 1
b := 2
m::add(a, b)}
This makes it easier to refactor code, because you only have to change the imports at the top.
By default, functions shadow other ones in the same alias, by order. To prevent shadowing, use a specific import. A specific import will shadow all other functions:
use math::{add} as m
use graphics as m // if `graphics` adds an `add` it will be shadowed
If you try to import two specific functions with same name, the last one will shadow the others:
use math::{add} as m
use graphics::{add} as m // `m::add` refers to the last one
Motivation
This is designed for:
Help navigation in a larger code base
Improve refactoring
Understand which function a call refers to
The text was updated successfully, but these errors were encountered:
Dyon has optional namespaces to organize code and avoid name collisions.
ns math::algebra::topology
)use
statements (e.g.use math::algebra::topology as m
)An optional namespace does not take away functions in the default namespace, but adds it to a new namespace that you can use when needed. This means you do not have to import functions from a namespace to use them. You can use the default namespace as usual. The optional namespace is used when there is a risk of naming collision.
To declare a namespace, use the syntax
ns <namespace>
. You can only have one namespace in the same file:The namespace can be nested, e.g.
ns math::algebra::topology
. It should be unique to avoid naming collision with other modules.To call a function using a namespace, you must add a
use
statement at the top, after thens
line. The use statement requires an alias, e.g.use math as m
. This alias is also required when calling, to avoid ambiguity with the default namespace:You can import specific functions, leaving out the others. Specific imports are put inside curly braces, e.g.
use math::{add, sub} as m
:You can rename specific functions:
Shared namespace aliases
You can use the same namespace alias for more than one import:
This makes it easier to refactor code, because you only have to change the imports at the top.
By default, functions shadow other ones in the same alias, by order. To prevent shadowing, use a specific import. A specific import will shadow all other functions:
If you try to import two specific functions with same name, the last one will shadow the others:
Motivation
This is designed for:
The text was updated successfully, but these errors were encountered: