0Pricing
Zig Academy · 课时

@TypeOf 与类型反射

检查类型,以保持代码的泛型特性。

@TypeOf 与类型反射 是 CoddyKit 上的免费 Zig Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Zig Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Zig Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Asking About Types

Generic code often needs to know the type of a value without you spelling it out. Zig answers that with the built-in @TypeOf. 🔍

@TypeOf Returns a Type

Give @TypeOf one or more expressions and it returns their type, computed entirely at compile time.

const T = @TypeOf(value);

It Does Not Run the Expression

The argument to @TypeOf is only inspected, never executed. Even a function call inside it is examined but not actually run.

Capture the Type of a Parameter

Inside a generic function you can name the type of any argument and reuse it, for example to declare a matching local variable.

fn echo(x: anytype) void {
    const T = @TypeOf(x);
    _ = T;
}

Get a Readable Name

The built-in @typeName turns a type into a readable string, which is handy for debug printing or error messages.

const name = @typeName(i32);

Deeper Reflection with @typeInfo

For full detail, @typeInfo returns a tagged union describing the type: whether it is an int, a struct, a pointer, and more.

const info = @typeInfo(i32);

Branch on the Kind of Type

Because the result is a tagged union, you can switch on it to handle integers, floats, and structs differently in one generic function.

switch (@typeInfo(T)) {
    .int => {},
    else => {},
}

Read an Integer's Bit Width

When a type is an integer, its info carries fields like bits and signedness, so you can adapt your logic to the exact width.

const bits = @typeInfo(u16).int.bits;

Reflection Keeps Code Generic

By inspecting types instead of hard-coding them, one function can correctly serve many input types. That is the heart of Zig reflection. ✨

All at Compile Time

Every reflection built-in runs during the build, so this introspection adds zero run-time overhead to the finished program.

List a Struct's Fields

For a struct, @typeInfo exposes a fields array you can loop over at compile time to read each field's name and type.

const fields = @typeInfo(Point).@"struct".fields;

Quick Check

Inside a generic function you have a value x and want its type. Which built-in gives it to you?

Recap

Use @TypeOf to learn a value's type and @typeInfo to inspect its structure. Switching on that info lets one function adapt to many types. 🎯

常见问题解答

「@TypeOf 与类型反射」课时是免费的吗?

是的 — 「@TypeOf 与类型反射」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Zig Academy 课程的其余内容,请升级到 CoddyKit PRO。 Zig Academy 课程共包含 4 节课。

「@TypeOf 与类型反射」这节课中我会学到什么?

检查类型,以保持代码的泛型特性。 你通过在浏览器中直接运行的动手代码来练习 Zig Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Zig Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Zig Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「@TypeOf 与类型反射」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Zig Academy 课中编写并运行代码吗?

能。每节 Zig Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 接收类型的函数
  2. 泛型数据结构
  3. @TypeOf 与类型反射
  4. anytype 参数
← 返回 Zig Academy