Version: 1.1.0
Initialization state
Mojo tracks whether a name is initialized the way it tracks types. A name is either ready to use or it is not.
Essentials
- A
varwith an immediate value starts initialized. - A
varwith only a type starts uninitialized. - Every branch must initialize before the name is read.
- Transferring a value with
^leaves the source uninitialized. - Early returns still have to cover the paths that read the name.
Example
def make(flag: Bool) -> Int:
var n: Int
if flag:
n = 1
else:
n = 2
return n
Keep in mind
This state is a compile-time fact. There is no runtime null hiding inside an uninitialized Int.