入口函数(Entry Functions)
入口函数(entry function)修饰符用于可以直接被调用的函数,例如在交易(transaction)中直接调用。它可以和其它的函数可见性修饰符配合使用, 例如 public
, 这样可以使函数被其他模块(module)调用, 或者配合 public(entry)
使用, 使得函数可以被 friend
模块调用。
English Version
An entry function visibility modifier allows a function to be called directly (eg in transaction). It is combinable with other visibility modifiers, such as public
which allows calling from other modules) and public(friend)
for calling from friend modules.
module examples::object {
use sui::transfer;
use sui::object::{Self, UID};
use sui::tx_context::TxContext;
struct Object has key {
id: UID
}
/// 被`public`修饰的的函数可以被任意模块调用
/// 被`entry`修饰的函数不可以返回值
/// If function is defined as public - any module can call it.
/// Non-entry functions are also allowed to have return values.
public fun create(ctx: &mut TxContext): Object {
Object { id: object::new(ctx) }
}
/// 入口函数不可以拥有返回值因为它们可以在交易中被直接调用,返回值也不可用
/// 如果入口`entry`函数如果没有同时被`public`修饰将不可以被其他模块调用
/// Entrypoints can't have return values as they can only be called
/// directly in a transaction and the returned value can't be used.
/// However, `entry` without `public` disallows calling this method from
/// other Move modules.
entry fun create_and_transfer(to: address, ctx: &mut TxContext) {
transfer::transfer(create(ctx), to)
}
}