Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

类型参数与能力约束

对泛型类型参数施加 能力约束(ability constraints),可以要求类型具备 copydropstorekey 等能力。若类型参数只作类型标签、不出现在字段中,则需使用 phantom,详见下一节 §8.3 phantom 类型参数

能力约束

对类型参数添加能力约束,要求传入的类型必须满足相应能力:

module book::generic_constraints;

public struct Copyable<T: copy + drop> has copy, drop {
    value: T,
}

public struct Storable<T: store> has store {
    value: T,
}

public fun duplicate<T: copy>(value: &T): T {
    *value
}

常见约束组合:

约束含义
T: dropT 可以被丢弃
T: copyT 可以被复制
T: copy + dropT 可复制和丢弃
T: storeT 可存储在全局对象中
T: key + storeT 可作为顶层对象

泛型与对象

在 Sui 中,泛型常与对象结合,实现通用对象容器(如 Container<T: store> has key, store),并通过 store 等能力约束保证类型可安全存储。

小结

  • 能力约束T: copy + dropT: store 等,确保类型参数满足所需能力
  • phantom:单独成章,见 §8.3
  • 泛型对象:结合 key/store 实现可存储的泛型对象