No way to create a tuple with comptime fields using the new `@Tuple` builtin.
bug
### Zig Version
0.16.0-dev.1470+32dc46aae
### Steps to Reproduce and Observed Behavior
Hi there!
I am writing a small game engine inspired in some ways by Bevy.
The key feature is automatic, fully-comptime argument resolution for systems/functions using the `@typeInfo`, `@Type`, and `@call` builtins.
However, while investigating how to port the codebase to the latest Zig version, I ran into a problem: it seems impossible to create a tuple with comptime fields using the new `@Tuple` builtin. This feature is required for comptime-only arguments resolution.
Below is a heavily simplified version of the code currently used in my engine:
```zig
fn system(a: u32, b: @TypeOf(.enum_lteral)) void {
std.debug.print("Args values: {d} and {s}\n", .{ a, @tagName(b) });
}
pub fn GetArgsTuple(func: anytype) type {
const args = @typeInfo(@TypeOf(func)).@"fn".params;
var args_array: [args.len]std.builtin.Type.StructField = undefined;
inline for (args, 0..) |arg, i| {
const ArgType = arg.type orelse unreachable;
args_array[i] = if (ArgType == @TypeOf(.enum_lteral))
std.builtin.Type.StructField{
.name = std.fmt.comptimePrint("{d}", .{i}),
.type = @TypeOf(.enum_lteral),
.default_value_ptr = @ptrCast(&.hello),
.alignment = @alignOf(@TypeOf(.enum_lteral)),
.is_comptime = true,
}
else
std.builtin.Type.StructField{
.name = std.fmt.comptimePrint("{d}", .{i}),
.type = ArgType,
.default_value_ptr = null,
.alignment = @alignOf(ArgType),
.is_comptime = false,
};
}
return @Type(.{ .@"struct" = std.builtin.Type.Struct{
.layout = .auto,
.fields = &args_array,
.decls = &.{},
.is_tuple = true,
} });
}
pub fn main() void {
const ArgsTuple = comptime GetArgsTuple(system);
var args: ArgsTuple = undefined;
inline for (0..args.len) |i| {
const ArgType = @TypeOf(args[i]);
if (ArgType == @TypeOf(.enum_lteral)) { // allready provided
continue;
} else if (ArgType == u32) {
args[i] = 2;
} else unreachable;
}
@call(.auto, system, args);
}
const std = @import("std");
```
Code snippet can be compiled using `0.15.0-dev.451+a843be44a`.
P.S.: If there are any other possible ways to handle comptime argument resolution, let me know.
### Expected Behavior
I expect that the rewriting should be done without losing **useful** functionality.
关闭于 2025-11-26 2 条评论