i have read this project line: 875: zig-clap/clap.zig at master · Hejsil/zig-clap · GitHub
fn Positionals(
comptime Id: type,
comptime params: []const Param(Id),
comptime value_parsers: anytype,
comptime multi_arg_kind: MultiArgKind,
) type {
var fields_len: usize = 0;
for (params) |param| {
const longest = param.names.longest();
if (longest.kind != .positional)
continue;
fields_len += 1;
}
var fields: [fields_len]std.builtin.Type.StructField = undefined;
var i: usize = 0;
for (params) |param| {
const longest = param.names.longest();
if (longest.kind != .positional)
continue;
const T = ParamType(Id, param, value_parsers);
const FieldT = switch (param.takes_value) {
.none => continue,
.one => ?T,
.many => switch (multi_arg_kind) {
.slice => []const T,
.list => std.ArrayListUnmanaged(T),
},
};
fields[i] = .{
.name = std.fmt.comptimePrint("{}", .{i}),
.type = FieldT,
.default_value_ptr = null,
.is_comptime = false,
.alignment = @alignOf(FieldT),
};
i += 1;
}
return @Type(.{ .@"struct" = .{
.layout = .auto,
.fields = &fields,
.decls = &.{},
.is_tuple = true,
} });
}
I have 3 question
1)
var fields: [fields_len]std.builtin.Type.StructField = undefined;
created on function stack but its address use in return
return @Type(.{ .@"struct" = .{
.layout = .auto,
.fields = &fields,
.decls = &.{},
.is_tuple = true,
} });
this have to be memory corruption but it works magically
-
how this return @Type returns something like slice or array on runtime? it looks paranormal
-
i read a lot of source code on
tigerbeetle
andbun
project. I have seen a lot of magic like these too. How can i deeply understand this concepts. one day something looks impossible but another day someone make it
For example, as i can read I cant write ‘add’ or ‘minus’ operator overloading like c++, python or others languages magic methods but @Vector can do this , maybe there is an hack for this maybe i can write builtin methods, I only need to know how can tiggerbeetle or bun coders know these? Where is the holly source?