use*_*713 1 substrate polkadot
FRAME2 存储使用以下语法定义:
#[pallet::storage]
type SomePrivateValue<T> = StorageValue<_, u32, ValueQuery>;
#[pallet::storage]
#[pallet::getter(fn some_primitive_value)]
pub(super) type SomePrimitiveValue<T> = StorageValue<_, u32, ValueQuery>;
Run Code Online (Sandbox Code Playgroud)
但存储无需 ValueQuery 关键字即可工作。
例如
#[pallet::storage]
type SomePrivateValue<T> = StorageValue<_, u32>;
Run Code Online (Sandbox Code Playgroud)
ValueQuery 有什么用?
同样设置默认值需要ValueStorage,而ValueStorage不允许使用getter函数。如何拥有设置默认值的 getter 函数?
https://substrate.dev/docs/en/knowledgebase/runtime/storage#default-values
编辑:文档现在托管在https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/index.html
正式文档StorageValue可以在这里找到https://paritytech.github.io/substrate/monthly-2021-09+1/frame_support/storage/types/struct.StorageValue.html
在那里您可以看到第三个泛型QueryKind默认是 type OptionQuery,当您不提供特定类型时,则OptionQuery使用 then 。
在该类型的方法实现中,StorageValue您可以看到它需要对泛型进行一些限制https://paritytech.github.io/substrate/monthly-2021-09+1/frame_support/storage/types/struct.StorageValue.html#impl
特别是它要求QueryKind: QueryKindTrait<Value, OnEmpty>,
这意味着 QueryKind 必须实现如何从存储中查询值。
目前有 2 个实现者:OptionQuery和ValueQuery: https: //paritytech.github.io/substrate/monthly-2021-09+1/frame_support/storage/types/trait.QueryKindTrait.html#implementors
OptionQuery以当在存储中找不到值时返回 None 的方式实现该特征。当在存储中找不到值时,ValueQuery以返回一些空值(由 generic 配置)的方式实现该特征。OnEmpty
因此,ValueQuery当您想获取一些“默认”值时,当您尝试从存储中获取一些值但没有值时,或者OptionQuery当您想获取 None 时,当您尝试从存储中获取一些值时,您可以使用没有价值。
后果在方法的签名中可见:该方法get将返回一个值或一个选项:https://paritytech.github.io/substrate/monthly-2021-09+1/frame_support/storage/types/struct。 StorageValue.html#method.get
| 归档时间: |
|
| 查看次数: |
426 次 |
| 最近记录: |