const std = @import("std");
pub fn main() !void {
const T = @TypeOf(.{.third = 3, .first = 1, .second = 2});
const s = @typeInfo(T).@"struct";
var field_names: [s.fields.len][]const u8 = undefined;
inline for (s.fields, 0..) |f, i| {
field_names[i] = f.name;
}
comptime sort(&field_names);
std.debug.print("{any}\n", .{field_names});
}
fn sort(values: [][]const u8) void {
std.mem.sort([]const u8, values, {}, struct {
fn sort(_: void, lhs: []const u8, rhs: []const u8) bool {
return std.mem.order(u8, lhs, rhs) == .lt;
}
}.sort);
}
Doesn't compile:
error: unable to evaluate comptime expression
comptime sort(&field_names);
If I take out the comptime from the call to sort, it works. But I need the fields sorted at comptime.