imports.rst 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. Imports
  2. =======
  3. The ``import`` directive is used to import items from other Solidity files. This can be useful to
  4. keep a single definition in one file, which can be used in multiple other files. For example,
  5. you could have an interface in one source file, which several contracts implement or use
  6. which are in other files. Solidity imports are somewhat similar to JavaScript ES6, however
  7. there is no export statement, or default export.
  8. The following items are always exported, which means they can be imported into
  9. another file.
  10. - global constants
  11. - struct definitions
  12. - enums definitions
  13. - event definitions
  14. - global functions
  15. - free standing functions
  16. - contracts, including abstract contract, libraries, and interfaces
  17. There are a few different flavours of import. You can specify if you want everything imported,
  18. or just a select few items. You can also rename the imports. The following directive imports only
  19. `foo` and `bar`:
  20. .. code-block:: solidity
  21. import {foo, bar} from "defines.sol";
  22. Solang will look for the file `defines.sol` in the same directory as the current file. You can specify
  23. more directories to search with the ``--importpath`` commandline option.
  24. Just like with ES6, ``import`` is hoisted to the top and both `foo` and `bar` are usuable
  25. even before the ``import`` statement. It is also possible to import everything from
  26. `defines.sol` by leaving the list out. Note that this is different than ES6, which would import nothing
  27. with this syntax.
  28. .. code-block:: solidity
  29. import "defines.sol";
  30. Another method for locating files is using import maps. This maps the first directory
  31. of an import path to a different location on the file system. Say you add
  32. the command line option ``--importmap @openzeppelin=/opt/openzeppelin-contracts/contracts``, then
  33. .. code-block:: solidity
  34. import "@openzeppelin/interfaces/IERC20.sol";
  35. will automatically map to `/opt/openzeppelin-contracts/contracts/interfaces/IERC20.sol`.
  36. Everything defined in `defines.sol` is now usable in your Solidity file. However, if an item with the
  37. same name is defined in `defines.sol` and also in the current file, you will get a warning. It is
  38. permitted to import the same file more than once.
  39. It is also possible to rename an import. In this case, only item `foo` will be imported, and `bar`
  40. will be imported as `baz`. This is useful if you have already have a `bar` and you want to avoid
  41. a naming conflict.
  42. .. code-block:: solidity
  43. import {bar as baz,foo} from "defines.sol";
  44. Rather than renaming individual imports, it is also possible to make all the items in a file
  45. available under a special import object. In this case, the `bar` defined in `defines.sol` can is
  46. now visible as `defs.bar`, and `foo` as `defs.foo`. As long as there is no previous item `defs`,
  47. there can be no naming conflict.
  48. .. code-block:: solidity
  49. import "defines.sol" as defs;
  50. There is another syntax, which does exactly the same.
  51. .. code-block:: solidity
  52. import * as defs from "defines.sol";