It seems like structs with only comptime stuct fields can coerce to compatible structs:
@compileLog(@as(Rational(i32), .{.num = 6, .den = 9}));
@compileLog(@TypeOf(.{.num = 6, .den = 9}));
Compile Log Output:
@as(types.Rational(i32), .{.num = 6, .den = 9})
@as(type, struct{comptime num: comptime_int = 6, comptime den: comptime_int = 9})
So I attempted to make use of this for obvious purposes:
fn ComptimeRationalFromString(comptime xstr: []const u8) type {
const sign, const str = if (std.mem.startsWith(u8, xstr, "-")) .{ -1, xstr[1..] } else .{ 1, xstr };
if (str.len == 0) return @TypeOf(comptimeRational(0, 1));
const dec_idx = (std.mem.indexOfScalar(u8, str, '.') orelse str.len - 1) + 1;
if (dec_idx == str.len) return struct {
comptime num: comptime_int = parseComptimeInt(str, 10),
comptime den: comptime_int = 1,
};
const num = parseComptimeInt(str[0..dec_idx - 1], 10);
const scale = std.math.powi(u65535, 10, str.len - dec_idx) catch |e| @compileError(@errorName(e) ++ ", what?");
const den = parseComptimeInt(str[dec_idx..], 10);
return struct {
comptime num: comptime_int = num * scale + den,
comptime den: comptime_int = scale * sign,
};
}
pub fn comptimeRationalFromString(comptime str: []const u8) ComptimeRationalFromString(str) {
return .{};
}
But it doesn’t work:
error: expected type 'types.Rational(i32)', found 'types.ComptimeRationalFromString("0.64")'
Why doesn’t it, and what do I need to do to make it or something similarly ergonomic work?
pub const bt_2020 = .{
cRS("0.708"), cRS("0.292"),
cRS("0.170"), cRS("0.797"),
cRS("0.131"), cRS("0.046"),
};
Ought to coerce to [6]struct{num: i32, den: i32}
, right?