Introspection Builtins
Throughout the book, we've already used the function @TypeOf. You know that @ means these are builtin functions, that is, functions built into the language. Early on, we also used @sizeOf(T) to inspect the size of a type.
You can find detailed information about all builtin functions at this URL: https://ziglang.org/documentation/master/#Builtin-Functions.
Some builtins that are especially useful for working with types and creating generics are:
|
@TypeOf (value) |
Returns the type of a value |
|
@typeInfo (T) |
Returns structured information about a type |
|
@hasField (T, “field”) |
Returns a boolean indicating whether the struct (type) has the field |
|
@typeName (T) |
Returns a string with the name of the type |
|
@compileError (s_msg) |
Triggers a compile-time error |
|
@field (T, “field”) |
Accesses fields for reading or modification |
All these type-inspection functions operate at compile time.