8000 Optional namespaces with shared aliases · Issue #434 · PistonDevelopers/dyon · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Optional namespaces with shared aliases #434
Closed
@bvssvni

Description

@bvssvni

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
 
731A
   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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0