Skip to content

Commit

Permalink
feat[docs]: add more detail to modules docs (#4087)
Browse files Browse the repository at this point in the history
- update technical notes in using modules (reference the VIP)
- note about linearization check
- exports of interfaces
  • Loading branch information
charles-cooper authored Jun 1, 2024
1 parent bb9129a commit 80dcee2
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion docs/using-modules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ The design of the module system takes inspiration from (but is not directly rela
* A module may be "used" many times
* A module which is "used" or its state touched must be "initialized" exactly once

To read more about the design background of Vyper's module system, please see its original `design document <https://github.com/vyperlang/vyper/issues/3722>`_.

.. _init-dependencies:

Initializing a module with dependencies
Expand All @@ -169,14 +171,19 @@ Sometimes, you may encounter a module which itself ``uses`` other modules. Vyper
# export all external functions from ownable_2step
exports: ownable_2step.__interface__
.. warning::
In normal usage, you should make sure that ``__init__()`` functions are called in dependency order. In the above example, you can get unexpected behavior if ``ownable_2step.__init__()`` is called before ``ownable.__init__()``! The compiler may enforce this behavior in the future.

.. _exporting-functions:

Exporting functions
===================

In Vyper, ``@external`` functions are not automatically exposed (i.e., included in the runtime code) in the importing contract. This is a safety feature, it means that any externally facing functionality must be explicitly defined in the top-level of the compilation target.

So, exporting external functions from modules is accomplished using the ``exports`` keyword. In Vyper, functions can be exported individually, or, a wholesale export of all the functions in a module can be done. The following are all ways of exporting functions from an imported module.
So, exporting external functions from modules is accomplished using the ``exports`` keyword. In Vyper, functions can be exported individually, or, a wholesale export of all the functions in an interface can be done. The special interface ``module.__interface__`` is a compiler-defined interface, which automatically includes all the functions in a module.

The following are all ways of exporting functions from an imported module.

.. code-block:: vyper
Expand All @@ -190,5 +197,11 @@ So, exporting external functions from modules is accomplished using the ``export
ownable_2step.accept_ownership,
)
# export all IERC20 functions from `base_token`
exports: base_token.IERC20
# export all external functions from `ownable_2step`
exports: ownable_2step.__interface__
.. note::
Any exported interfaces must be implemented by the module. For example, in the above example, ``base_token`` must contain ``implements: IERC20``, or else the compiler will raise an error.

0 comments on commit 80dcee2

Please sign in to comment.