Abilities: Introduction
Move has a unique type system which allows customizing type abilities.
In the previous section, we introduced the struct
definition and how to use it.
However, the instances of the Artist
and Record
structs had to be unpacked for the code to
compile. This is default behavior of a struct without abilities.
Throughout the book you will see chapters with name
Ability: <name>
, where<name>
is the name of the ability. These chapters will cover the ability in detail, how it works, and how to use it in Move.
What are Abilities?
Abilities are a way to allow certain behaviors for a type. They are a part of the struct declaration and define which behaviours are allowed for the instances of the struct.
Abilities syntax
Abilities are set in the struct definition using the has
keyword followed by a list of abilities.
The abilities are separated by commas. Move supports 4 abilities: copy
, drop
, key
, and
store
, each of them is used to define a specific behaviour for the struct instances.
/// This struct has the `copy` and `drop` abilities.
struct VeryAble has copy, drop {
// field: Type1,
// field2: Type2,
// ...
}
Overview
A quick overview of the abilities:
All of the built-in types, except references, have
copy
,drop
andstore
abilities. References havecopy
anddrop
.
copy
- allows the struct to be copied. Explained in the Ability: Copy chapter.drop
- allows the struct to be dropped or discarded. Explained in the Ability: Drop chapter.key
- allows the struct to be used as a key in a storage. Explained in the Ability: Key chapter.store
- allows the struct to be stored in structs with the key ability. Explained in the Ability: Store chapter.
While it is important to mention them here, we will go in detail about each ability in the following chapters and give a proper context on how to use them.
No abilities
A struct without abilities cannot be discarded, or copied, or stored in the storage. We call such a struct a Hot Potato. It is a joke, but it is also a good way to remember that a struct without abilities is like a hot potato - it can only be passed around and requires special handling. Hot Potato is one of the most powerful patterns in Move, we go in detail about it in the Hot Potato Pattern chapter.
Further reading
- Type Abilities in the Move Reference.