Solidity Importing

Solidity Importing Main Tips

  • Solidity allows import statements, making libraries and other related features viable.
  • Importing in Solidity is fairly well implemented and important.

Solidity Importing

Importing in Solidity uses statements that very similar to JavaScript past version ES6.

It should be noted though, that Solidity does not support export statements.

Globally, import statements like these work:

import "importedFile";

The statement shown above will import all symbol from, and imported into, importedFile, into the global scope. This is different from ES6, but it’s backwards-compatible to Solidity.

The line shown below will created a symbol called importedSymbol, containing the global symbols from importedFile.

import * as importedSymbol from "importedFile";

The code below will create two symbols – alias, referencing symbol and symbol2 referencing symbol 2 from importedFile.

import {symbol as alias, symbol2} from "importedFile";

Another line of syntax for Solidity, that is rather convenient:

import "importedFile" as importedSymbol;

The line below is the equivalent of this in ES6:

import * as importedSymbol from "importedFile";

Solidity Importing Paths

Writing the path to the file you want to import follows a few simple syntax rules.

Firstly, you should know that the symbol is the directory separator, . is the current directory, and .. is the parent directory. If or .. is followed by any character except for /, it is not considered the current . or parent .. category. Each path is treated as an absolute path, except when it starts with either . or ..

For example:

To import a file from inside the same directory as the source file, the syntax you should use would look like this:

import "./x" as x;

You might also try writing it like this:

import "x" as x;

But that could reference a different file in the global “include” directory.

Either way, the compiler still plays an important role in how file paths are resolved.

You should also keep in mind that the hierarchy of the directory does not have to refer strictly to your local filesystem, it can also refer to externally discovered resources via http, ipfs, git, etc.

Solidity Importing In Remix

In the case of Remix, automatic remapping to github is provided, in addition to automatically retrieving the file over the network.

It is also possible to import itterable mapping by using the example below:

Read previous post:
Solidity Comments

Solidity Comments Main Tips Comments in Solidity, like in most other programming languages, are used for providing hints throughout the code...