If you have ever used another programming language (C/C++, Python, Java, Ruby, Lisp, etc.), it is likely that you have encountered null or nil before.
The use of null or nil is how these languages indicate that a particular variable has no value.
However, this makes accidentally using a null variable an easy (and frequent) mistake to make.
As you might imagine, trying to call a function that isn't there, or access a value that doesn't exist can lead to all sorts of bugs and crashes.
The creator of null went so far as to call it his 'billion-dollar mistake'.
Option TypeTo avoid these problems, Cairo variables can use the Option enum to safely represent the lack of a value.
This enum has two variants: None, Cairo's null-equivalent; and Some(T), where T is a value of any type.
It looks like this:
enum Option<T> {
None,
Some(T),
}
You can think of Option as a layer of safety between you and the problems that null-references can cause, while still retaining their conceptual usefulness.
Option
Setting a variable to None is fairly straightforward:
let nothing: Option<u32> = Option::None; // Variable nothing is set to None
However, if you wish for the Option type to carry a value, you cannot assign this value directly.
An Option type variable and, say, an i32 type variable are not equivalent.
You need to use Some:
let wrong_way: Option<i32> = -4; // This will not work
let right_way: Option<i32> = Option::Some(-4); // This will work
let another_right_way = Option::Some(-4); // Compiler infers that this is Option<i32>
For the same reason the following will not work:
let number = 47;
let option_number = Option::Some(15);
let compile_error = number + option_number; // Cannot add an i32 and an Option<i32> - they are of different types
To get the value contained by Some, you need to first check that it exists:
let mut some_words = Option::Some("choose something to say"); // some_words set to something
match some_words {
Option::Some(str) => println!("Here, we will {}", str),
Option::None => println!("I've got nothing to say"),
} // Prints, "Here, we will choose something to say"
some_words = Option::None; // some_words now set to None
// Exactly the same match block as above
match some_words {
Option::Some(str) => println!("Here, we will {}", str),
Option::None => println!("I've got nothing to say"),
} // Prints, "I've got nothing to say"
Cairo has other tools other than match available for checking and accessing values contained within Option, but match should be familiar to you now.
Consider this a demonstration of why Cairo uses Option instead of a null-reference.
The point is that you must check whether or not the Option variable is Some (in which case you can go ahead and extract and use the value contained within), or None.
Otherwise your program will not compile; the compiler keeps you safe from null.
You're working on implementing a role-playing game. The player's character is represented by the following:
pub struct Player {
health: u32,
mana: Option<u32>,
level: u32,
}
Players in this game must reach level 10 before they unlock a mana pool, which they can use to cast spells.
Before that point, the Player's mana is Option::None.
You're working on two pieces of functionality in this game: the revive mechanic and the spell casting mechanic.
The revive method should check to ensure that the Player is indeed dead (their health has reached 0), and if they are, the method should return a new Player instance with 100 health.
The revive method should preserve the Player's level.
If the Player's level is 10 or above, they should be revived with 100 mana.
If the Player's level is below 10, their mana should be Option::None.
let dead_player = Player { health: 0, mana: Option::None, level: 2 };
dead_player.revive()
// Returns Player { health: 100, mana: Option::None, level: 2 }
If the revive method is called on a Player whose health is 1 or above, then the method should return Option::None.
let alive_player = Player { health: 1, mana: Option::Some(15), level: 11 };
alive_player.revive()
// Returns Option::None
The cast_spell method takes a mutable reference to the Player as well as a mana_cost parameter indicating how much mana the spell costs.
It returns the amount of damage that the cast spell performs.
If the player does not have access to a mana pool, attempting to cast the spell must decrease their health by the mana cost of the spell. The damage returned must be 0.
let not_a_wizard_yet = Player { health: 79, mana: Option::None, level: 9 };
assert_eq!(not_a_wizard_yet.cast_spell(5), 0)
assert_eq!(not_a_wizard_yet.health, 74);
assert_eq!(not_a_wizard_yet.mana, Option::None);
If the player has a mana pool but insufficient mana, the method should not affect the pool. The damage returned must be 0.
let low_mana_wizard = Player { health: 93, mana: Option::Some(3), level: 12 };
assert_eq!(low_mana_wizard.cast_spell(10), 0);
assert_eq!(low_mana_wizard.health, 93);
assert_eq!(low_mana_wizard.mana, Option::Some(3));
Otherwise, the spell is cast successfully.
The mana_cost should be deducted from the Player's mana pool.
The damage returned must be two times the mana cost of the spell.
let wizard = Player { health: 123, mana: Option::Some(30), level: 18 };
assert_eq!(wizard.cast_spell(10), 20);
assert_eq!(wizard.health, 123);
assert_eq!(wizard.mana, Option::Some(20));
Have fun!
Sign up to Exercism to learn and master Cairo with 25 concepts68 exercises, and real human mentoring, all for free.