Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional namespaces with shared aliases #434

Closed
bvssvni opened this issue Jan 17, 2017 · 0 comments
Closed

Optional namespaces with shared aliases #434

bvssvni opened this issue Jan 17, 2017 · 0 comments
Assignees

Comments

@bvssvni
Copy link
Member

bvssvni commented Jan 17, 2017

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`
fn add(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

fn main() {
    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

fn main() {
    a := 1
    b := 2
    m::add(a, b) // prefix is still required.
}

You can rename specific functions:

use math::{add as addition} as m

fn main() {
    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

fn main() {
    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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant