Uses and Aliases

The use syntax can be used to create aliases to members in other modules. use can be used to create aliases that last either for the entire module, or for a given expression block scope.

Syntax

There are several different syntax cases for use. Starting with the most simple, we have the following for creating aliases to other modules

use <address>::<module name>;
use <address>::<module name> as <module alias name>;

For example

script {
  use std::vector;
  use std::vector as V;
}

use std::vector; introduces an alias vector for std::vector. This means that anywhere you would want to use the module name std::vector (assuming this use is in scope), you could usevector instead. use std::vector; is equivalent to use std::vector as vector;

Similarly use std::vector as V; would let you use V instead of std::vector

module 0x42::example {
  use std::vector;
  use std::vector as V;

  fun new_vecs(): (vector<u8>, vector<u8>, vector<u8>) {
    let v1 = std::vector::empty();
    let v2 = vector::empty();
    let v3 = V::empty();
    (v1, v2, v3)
  }
}

If you want to import a specific module member (such as a function, struct, or constant). You can use the following syntax.

For example

This would let you use the function std::vector::empty without full qualification. Instead, you could use empty and empty_vec respectively. Again, use std::vector::empty; is equivalent touse std::vector::empty as empty;

If you want to add aliases for multiple module members at once, you can do so with the following syntax

For example

If you need to add an alias to the Module itself in addition to module members, you can do that in a single use using Self. Self is a member of sorts that refers to the module.

For clarity, all the following are equivalent:

If needed, you can have as many aliases for any item as you like

Inside a module

Inside a module all use declarations are usable regardless of the order of declaration.

The aliases declared by use in the module usable within that module.

Additionally, the aliases introduced cannot conflict with other module members. SeeUniqueness for more details

Inside an expression

You can add use declarations to the beginning of any expression block

As with let, the aliases introduced by use in an expression block are removed at the end of that block.

Attempting to use the alias after the block ends will result in an error

Any use must be the first item in the block. If the use comes after any expression or let, it will result in a parsing error

Naming rules

Aliases must follow the same rules as other module members. This means that aliases to structs or constants must start with A to Z

Uniqueness

Inside a given scope, all aliases introduced by use declarations must be unique.

For a module, this means aliases introduced by use cannot overlap

And, they cannot overlap with any of the module's other members

Inside an expression block, they cannot overlap with each other, but they canshadow other aliases or names from an outer scope

Shadowing

use aliases inside of an expression block can shadow names (module members or aliases) from the outer scope. As with shadowing of locals, the shadowing ends at the end of the expression block;

Unused Use or Alias

An unused use will result in an error

Last updated