Skip to content

struct Offer<T: key + store> has key, store { #184

Answered by Demian101
Demian101 asked this question in Q&A
Discussion options

You must be logged in to vote

https://move-book.com/cn/advanced-topics/understanding-generics.html

abilities 限制符

我们已经学习了 abilities,它们可以作为泛型的限制符来使用,限制符的名称和 ability 相同。

fun name<T: copy>() {} // allow only values that can be copied
fun name<T: copy + drop>() {} // values can be copied and dropped
fun name<T: key + store + drop + copy>() {} // all 4 abilities are present
也可以在结构体泛型参数中使用:

struct name<T: copy + drop> { value: T } // T can be copied and dropped
struct name<T: stored> { value: T } // T can be stored in global storage
请记住 + 这个语法符号,第一眼看上去可能不太适应,因为很少有语言在关键字列表中使用 +。

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by Demian101
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant