-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
Learning Zig
By :
Imagine you’re writing a piece of code that has multiple conditional branches, each returning a slightly different type, but in the end, you want to produce a single uniform type. Or consider a switch expression, with each branch returning something different yet somehow compatible. In such cases, Zig engages in a subtle dance called peer type resolution.
Peer type resolution occurs in several places, including the following:
if expressionswhile expressionsfor expressionsswitch expressionsbreak statements returning different typesWhenever Zig encounters multiple “peer” types. Types from different branches that need to unify into one result type. It attempts to find a type that all peers can safely coerce into. This ensures that the entire expression has a consistent type without you having to explicitly specify it every time.