10.2 属性(Attributes)

The Sway compiler supports a list of attributes that perform various operations that are useful for building, testing and documenting Sway programs. Below is a list of all available attributes:

Sway 编译器支持一系列属性,这些属性执行各种对构建、测试和记录 Sway 程序是很有用的操作。以下是所有可用属性的列表:

允许(Allow)

The #[allow(dead_code)] attribute overrides the check for dead code so that violations will go unreported.

#[allow(dead_code)] 属性会覆盖对死代码的检查,这样违规行为就不会被报告。

文档(Doc)

The #[doc(..)] attribute specifies documentation.

#[doc(..)] 属性指定文档。

Line doc comments beginning with exactly three slashes ///, are interpreted as a special syntax for doc attributes. That is, they are equivalent to writing #[doc("...")] around the body of the comment, i.e., /// Foo turns into #[doc("Foo")]

以三个斜杠///开头的文档注释,被解释为文档属性的特殊语法。也就是说,它们等同于在评论正文周围写上#[doc("...")],即,/// Foo 变成#[doc("Foo")]

Line comments beginning with //! are doc comments that apply to the module of the source file they are in. That is, they are equivalent to writing #![doc("...")] around the body of the comment. //! module level doc comments should be at the top of Sway files.

//! 开头的行注释,适用于其所在的源文件模块的文档注释。也就是说,它们等同于在正文周围编写#![doc("...")]的评论。 //! 模块级文档注释应该位于 Sway 文件的顶部。

Documentation can be generated from doc attributes using forc doc.

可以使用 forc doc 从 doc 属性生成文档。

内联(Inline)

The inline attribute suggests that a copy of the attributed function should be placed in the caller, rather than generating code to call the function where it is defined.

内联属性建议将属性函数的副本放在调用者中,而不是生成代码来调用函数定义的地方。

Note: The Sway compiler automatically inlines functions based on internal heuristics. Incorrectly inlining functions can make the program slower, so this attribute should be used with care. 注意:Sway 编译器会根据内部试探法自动内联函数。不正确的内联函数会使程序变慢,因此应谨慎使用此属性。

The #[inline(never)] attribute suggests that an inline expansion should never be performed.

#[inline(never)] 属性 建议 永远不应执行内联扩展。

The #[inline(always)] attribute suggests that an inline expansion should always be performed.

#[inline(always)] 属性 建议 应始终执行内联扩展。

Note: #[inline(..)] in every form is a hint, with no requirements on the language to place a copy of the attributed function in the caller. 注意#[inline(..)] 在每种形式中都是一个提示,在语言上没有 requirements 在调用方中放置属性函数的副本。

可支付(Payable)

The lack of #[payable] implies the method is non-payable. When calling an ABI method that is non-payable, the compiler emits an error if the amount of coins forwarded with the call is not guaranteed to be zero. Note that this is strictly a compile-time check and does not incur any runtime cost.

缺少 #[payable] 意味着该方法是不可支付的。当调用非支付的 ABI 方法时,如果不能保证通过调用转发的代币数量为零,编译器会发出错误。请注意,这完全是编译时检查,不会产生任何运行时成本。

存储(Storage)

In Sway, functions are pure by default but can be opted into impurity via the storage function attribute. The storage attribute may take read and/or write arguments indicating which type of access the function requires.

在 Sway 中,函数默认是纯函数,但可以通过 storage 函数属性选择不纯。 storage 属性可以采用 read 和/或 write 参数,来指明函数需要哪种类型的访问。

The #[storage(read)] attribute indicates that a function requires read access to the storage.

#[storage(read)] 属性表示函数需要对存储的读取权限。

The #[storage(write)] attribute indicates that a function requires write access to the storage.

#[storage(write)] 属性表示函数需要对存储的写访问权限。

More details in Purity.

纯度 中有更多详细信息。

测试(Test)

The #[test] attribute marks a function to be executed as a test.

#[test] 属性标记要作为测试执行的函数。

The #[test(should_revert)] attribute marks a function to be executed as a test that should revert.

#[test(should_revert)] 属性将要执行的函数标记为应还原的测试。

More details in Unit Testing.

单元测试 中有更多详细信息。

Last updated