Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat[docs]: add more detail to modules docs #4087

Merged
merged 5 commits into from
Jun 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Loading