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
  1. 6. 高级概念 (Advanced Concepts)

6.1 高级类型 (Advanced Types)

用类型别名创建类型同义词 (Creating Type Synonyms with Type Aliases)

Sway provides the ability to declare a type alias to give an existing type another name. For this we use the type keyword. For example, we can create the alias Kilometers to u64 like so:

Sway提供了声明一个类型别名的能力,进而给现有的类型另一个名字。为此我们使用type关键字。例如,我们可以创建Kilometers到u64的别名,像这样:

type Kilometers = u64;

Now, the alias Kilometers is a synonym for u64. Note that Kilometers is not a separate new type. Values that have the type Kilometers will be treated the same as values of type u64:

现在,别名 Kilometers是 u64的 _同义词。请注意,Kilometers不是 **一个独立的新类型 **。具有 Kilometers类型的值将被视为与 u64类型的值相同:

    let x: u64 = 5;
    let y: Kilometers = 5;
    assert(x + y == 10);

Because Kilometers and u64 are the same type, we can add values of both types and we can pass Kilometers values to functions that take u64 parameters. However, using this method, we don’t get the type checking benefits that we get from introducing a separate new type called Kilometers. In other words, if we mix up Kilometers and i32 values somewhere, the compiler will not give us an error.

因为Kilometers和u64是同一类型,我们就可以添加两种类型的值,我们可以将Kilometers的值传递给接受u64参数的函数。然而,使用这种方法,我们无法得到类型检查的好处,因为我们引入了一个名为 Kilometers的 独立的 新类型。换句话说,如果我们把Kilometers和i32的值混在一起,编译器不会给我们一个错误。

The main use case for type synonyms is to reduce repetition. For example, we might have a lengthy array type like this:

类型同义词的主要使用情况是减少重复。例如,我们可能有一个很长的数组类型,像这样:

[MyStruct<u64, b256>; 5]

Writing this lengthy type in function signatures and as type annotations all over the code can be tiresome and error prone. Imagine having a project full of code like this:

将这种冗长的类型写在函数签名中,并作为类型注释在代码中随处可见,会让人感到厌烦和容易出错。想象一下,有一个项目充满了这样的代码:

fn foo_long(array: [MyStruct<u64, b256>; 5]) -> [MyStruct<u64, b256>; 5] {
    array
}

A type alias makes this code more manageable by reducing the repetition. Below, we’ve introduced an alias named MyArray for the verbose type and can replace all uses of the type with the shorter alias MyArray:

类型别名通过减少重复而使这段代码更容易管理。下面,我们为冗长的类型引入了一个名为MyArray的别名,可以用更短的别名MyArray来代替该类型的所有应用:

fn foo_long(array: [MyStruct<u64, b256>; 5]) -> [MyStruct<u64, b256>; 5] {
    array
}

This code is much easier to read and write! Choosing a meaningful name for a type alias can help communicate your intent as well.

这段代码读起来和写起来都容易多了! 为类型别名选择一个有意义的名字,也有助于传达你的意图。

Previous6. 高级概念 (Advanced Concepts)Next6.2 通用类型 (Generic Types)

Last updated 1 year ago