Member-only story
Solidity Tutorial: All About Imports
Dive into import statements with Solidity
Table of Contents
Modules and Code Modularity.Solidity Imports.Types of Solidity Import Syntax.
* Global Import
* Specific ImportsImport Aliases.
* Global Alias
* Alias SpecificWhich import syntax to use?What can you import from a Solidity file?Solidity Import Syntax Cheatsheet
Modules and Code Modularity
Before diving into Import statements in Solidity, let’s first understand modular programming. The concept of modules is very old. It first appeared 44 years ago with the programming languages Modula-2 and Pascal.
The idea behind modules is to break code into reusable components. You group functionalities in a module file and expose them to other files so that these other files can use them.
A module system helps organise your code by grouping variables and functions that make sense together in one single file.
Let’s take the example of modules in Javascript (ES6). In ES6, modules are files that export one or more values (objects, functions or variables). Then, any other Javascript module can use the functionality offered by this file by importing it (Copes, 2018).
Solidity Imports
The idea of importing files in Solidity is very similar to the concept of modules described above. It helps modularise your smart contracts by:
- Creating reusable pieces that other files can import.
- Making it easier to understand and digest the entire Solidity codebase of your project.
- Making it easier to work with the “Solidity modules” by focusing on smaller files (useful when debugging).
You can group and define variables (like constant
, enum
, struct
defined at the file level) and other Solidity objects (like contract
, interface
or library
) into a single file. Only once. And you can then import them and use them in other Solidity files using the import
syntax.