beginner
+150 XP

Mastering Move Types

Deep dive into primitive types, addresses, type casting, and overflow safety

Lesson Syllabus

Integer Types Deep Dive
🔢

The Full Integer Family

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!

⚖️

Choosing the Right Type

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.

🚨

Overflow: The Silent Killer

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 Protection

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!

Addresses in Move
📍

What is an Address?

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.

✏️

Address Format

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.

🏷️

Named Addresses

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.

🔐

Working with Addresses

You can compare addresses, store them in structs, and use them to check permissions. Common pattern: check if caller is authorized.

Type Casting & Operations
🔄

What is Type Casting?

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 from Larger to Smaller

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.

Why Casting Matters

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.

📝

Type Annotations

Sometimes you don't need 'as' - you can let Move infer the type or use type annotations. Explicit casting is clearer though!