Unwrap Safely with if and orelse
Handle null without surprises.
You Must Unwrap First
An optional is not the value inside it. Before you can use the contents you have to unwrap it and deal with the null case.
Capture with if
An if on an optional can bind the inner value with a pipe capture. The body runs only when the optional is not null.
if (maybe) |value| {
use(value);
}All lessons in this course
- Declaring Optional Types
- Unwrap Safely with if and orelse
- Optional Pointers and ?*T
- null Versus undefined