Sway编程语言-更新中(The Sway Programming Language-Updatin
  • Sway编程语言(The Sway Programming Language)
  • 1. 导言(Introduction)
    • 1.1 安装(Installation)
    • 1.2 Sway快速入门 (Sway Quickstart)
    • 1.3 Fuel工具链 (The Fuel Toolchain)
    • 1.4 一个Forc项目 (A Forc Project)
    • 1.5 标准库 (Standard Library)
  • 2. 示例(Example)
    • 2.1计数器(Counter)
    • 2.2子货币(Subcurrency)
    • 2.3 FizzBuzz
    • 2.4 钱包智能合约(Wallet Smart Contract)
  • 3.Sway编程类型(Sway Program Types)
    • 3.1 合约(Contracts)
    • 3.2 库 (Libraries)
    • 3.3 脚本(Scripts)
    • 3.4 谓词 (Predicates)
  • 4. Sway语言基础 (Sway Language basics)
    • 4.1 变量 (Variables)
    • 4.2 内置类型(Built-in Types)
    • 4.3 常用库类型(Commonly Used Library Types)
    • 4.4 区块链类 (Blockchain Types)
    • 4.5 函数 (Functions)
    • 4.6 结构、元祖和穷举 (Structs, Tuples, and Enums)
    • 4.7 方法和关联函数 (Methods and Associated Functions)
    • 4.8 注释和日志 (Comments and Logging)
    • 4.9 控制流 (Control Flow)
  • 5. 用Sway部署区块链 (Blockchain Development with Sway)
    • 5.1 哈希和加密学 (Hashing and Cryptography)
    • 5.2 合约存储(Contract Storage)
    • 5.3 函数纯度 (Function Purity)
    • 5.4 标识符(Identifiers)
    • 5.5 原生资产(Native Assets)
    • 5.6 访问控制 (Access Control)
    • 5.7 调用合约(Calling Contracts)
  • 6. 高级概念 (Advanced Concepts)
    • 6.1 高级类型 (Advanced Types)
    • 6.2 通用类型 (Generic Types)
    • 6.3 特征 (Traits)
    • 6.4 集 (Assembly)
  • 7. 一般集聚 (Common Collections)
    • 7.1 堆上的向量(Vectors on the Heap)
    • 7.2 存储向量 (Storage Vectors)
    • 7.3 存储映射 (Storage Maps)
  • 8.测试(Testing)
    • 8.1 单元测试(Unit Testing)
    • 8.2 用Rust来测试 (Testing with Rust)
  • 9.应用前端开发 (Application Frontend Development)
    • 9.1 TypeScript SDK
  • 10.Sway应用(Sway Reference)
    • 10.1 编译器内部函数(Compiler Intrinsics)
    • 10.2 属性(Attributes)
    • 10.3 风格向导(Style Guide)
    • 10.4 已知各类问题(Known Issues and Workarounds)
    • 10.5 与Solidity的不同之处 (Differences From Solidity)
    • 10.6 与Rust的不同之处 (Differences From Rust)
    • 10.7 向Sway贡献 (Contributing To Sway)
  • 11. Forc引用 (Forc Reference)
    • 11.1清单参考 (Manifest Reference)
    • 11.2 工作区(Workspaces)
    • 11.3 依赖(Dependencies)
    • 11.4 命令(Commands)
      • 11.4.1 forc-addr2line
      • 11.4.2 forc-build
      • 11.4.3 forc-check
Powered by GitBook
On this page
  • 允许(Allow)
  • 文档(Doc)
  • 内联(Inline)
  • 可支付(Payable)
  • 存储(Storage)
  • 测试(Test)
  1. 10.Sway应用(Sway Reference)

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)] 属性表示函数需要对存储的写访问权限。

测试(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)] 属性将要执行的函数标记为应还原的测试。

Previous10.1 编译器内部函数(Compiler Intrinsics)Next10.3 风格向导(Style Guide)

Last updated 1 year ago

More details in .

中有更多详细信息。

More details in .

中有更多详细信息。

Purity
纯度
Unit Testing
单元测试