Deep dive into primitive types, addresses, type casting, and overflow safety
Move has 5 unsigned integer types: u8, u16, u32, u64, u128. Each type can hold larger numbers but uses more memory. Choose the smallest type that fits your needs!
Use the smallest type that safely fits your values. Smaller types save blockchain storage (which costs money!). But be careful - if a value might grow, choose a larger type.
What happens when you exceed a type's maximum? In some languages, values "wrap around" silently (256 becomes 0). Move ABORTS instead - this prevents critical bugs in smart contracts!
Underflow is when you subtract below zero. Since Move only has unsigned integers (no negatives), this also causes an abort. This prevents balance bugs in token contracts!
An address uniquely identifies an account on the blockchain. Think of it like a bank account number. In Move, addresses identify users, modules, and stored objects.
Addresses must start with 0x followed by hexadecimal digits. They can be short (0x1) or long (0x123...). Leading zeros can be omitted: 0x1 and 0x01 are the same.
Instead of hard-coding hex values, you can use named addresses (defined in Move.toml). This makes code more readable and portable across different networks.
You can compare addresses, store them in structs, and use them to check permissions. Common pattern: check if caller is authorized.
Type casting converts a value from one type to another. Use the 'as' keyword. Move only allows safe casts - you can't accidentally lose data.
Casting to a smaller type is allowed but dangerous! If the value doesn't fit, Move aborts. Always ensure the value fits before casting down.
Different types can't be mixed in operations. You need to cast to the same type first. Common pattern: cast smaller type to larger, do math, result is larger type.
Sometimes you don't need 'as' - you can let Move infer the type or use type annotations. Explicit casting is clearer though!