liveBook · Manning

Type hints

As explained in Systems Programming with Zig

A type annotation is source-code syntax that explicitly specifies the type of a value, variable, parameter, or other program element.

Zig can infer a variable’s type when its value makes the type clear. When a value like 42 could have several types, an explicit annotation such as i32 removes the ambiguity. The compiler requires it to determine every variable and expression’s type. section 2.1.1

Use annotations with Zig’s type rules in mind:

  • Annotate when context does not determine a value’s type; i32 is one suitable choice for 42. section 2.1.1
  • Do not add annotations just for ceremony: Zig can infer many variable types. section 2.1.1
  • An annotation does not make implicit conversions permissive; Zig requires explicit coercions. section 2.1.1
Table 2.1 Basic Zig types
Name Explanation

bool

Logical type. true or false.

i32, i64, i17 etc. (i followed by a number of bits)

Signed (two’s-complement) integer type. In practice, you will mostly use i32 and i64, but integers can have any number of bits, from 0 to 65535.

u8, u32, u64, u23 etc. (u followed by a number of bits)

Unsigned integer type. In practice, you will mostly use u8 and maybe u32, but unsigned integers can have any number of bits, from 0 to 65535.

usize and isize

Special unsigned and signed integer types intended for usage as indices or offsets. These are equivalent to Rust’s usize and isize, and C/C++'s size_t and isize_t.

f16, f32, f64, f80, f128

Standard floating point types.

void

You can think of this as a type that represents nothing, nada, zilch. More formally, there is only one possible value of void. For the C programmers out there, Zig’s void type is similar to C’s void return type, but has nothing to do with C’s void * type.

type

Types in Zig are actually values, too! More specifically, type annotations in Zig are expressions like any other expression. With comptime, we can even create functions that generate new types, a common pattern for generic data structures.

comptime_int, comptime_float

Special types, mainly for integer and float constants, that can’t exist at runtime. These are arbitrarily precise, so that they can coerce to any integer or float type that can hold them. For now, you can treat this as a curiosity.

Compare Zig’s basic types, including signed and unsigned integers, floats, bool, and void. section 2.1.2

How Systems Programming with Zig covers it

How deep it goes
Works through inference and an ambiguous integer example, without detailing annotation syntax for other program elements.
Examples
Uses the integer 42 to motivate i32 and surveys Zig’s basic types.
What you can do after
Evaluate when context leaves a variable’s type ambiguous and an explicit annotation is needed.
How it connects
Connects type annotations to type inference, static typing, strong typing, and basic types.

Also explained in

Compare how 22 books explain type hints →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Systems Programming with Zig. The section links point to the parts of the book it draws on.