Friends
Friends
The friend
syntax is used to declare modules that are trusted by the current module. A trusted module is allowed to call any function defined in the current module that have the public(friend)
visibility. For details on function visibilities, please refer to the Visibility section in Functions.
Friend declaration
A module can declare other modules as friends via friend declaration statements, in the format of
friend <address::name>
— friend declaration using fully qualified module name like the example below, orfriend <module-name-alias>
— friend declaration using a module name alias, where the module alias is introduced via theuse
statement.
A module may have multiple friend declarations, and the union of all the friend modules forms the friend list. In the example below, both 0x42::B
and 0x42::C
are considered as friends of 0x42::A
.
Unlike use
statements, friend
can only be declared in the module scope and not in the expression block scope. friend
declarations may be located anywhere a top-level construct (e.g., use
, function
, struct
, etc.) is allowed. However, for readability, it is advised to place friend declarations near the beginning of the module definition.
Note that the concept of friendship does not apply to Move scripts:
A Move script cannot declare
friend
modules as doing so is considered meaningless: there is no mechanism to call the function defined in a script.A Move module cannot declare
friend
scripts as well because scripts are ephemeral code snippets that are never published to global storage.
Friend declaration rules
Friend declarations are subject to the following rules:
A module cannot declare itself as a friend.
Friend modules must be known by the compiler
Friend modules must be within the same account address. (Note: this is not a technical requirement but rather a policy decision which may be relaxed later.)
Friends relationships cannot create cyclic module dependencies.
Cycles are not allowed in the friend relationships, e.g., the relation
0x2::a
friends0x2::b
friends0x2::c
friends0x2::a
is not allowed. More generally, declaring a friend module adds a dependency upon the current module to the friend module (because the purpose is for the friend to call functions in the current module). If that friend module is already used, either directly or transitively, a cycle of dependencies would be created.The friend list for a module cannot contain duplicates.
Last updated