I have some comptime code that seems way longer than it needs to be. Comments are included about why each piece exists, and what errors I ran into that led me to put it there. I feel like there must be a better way to do this.
fn getBaseQuantities(comptime bases: []const TypeValuePair) []const TypeValuePair {
comptime {
var basesReduced: []const TypeValuePair = &.{};
for (0..bases.len) |i| {
switch (interface.satisfiesInterface(CompositeQuantity, bases[i].t)) {
.Satisfies => {
for (getBaseQuantities(bases[i].t.bases)) |baseToAdd| {
basesReduced = basesReduced ++ [_]TypeValuePair{.{ .t = baseToAdd.t, .v = baseToAdd.v * bases[i].v }};
}
continue;
},
.Fails => {},
}
switch (interface.satisfiesInterface(BaseQuantity, bases[i].t)) {
.Satisfies => {
basesReduced = basesReduced ++ [_]TypeValuePair{bases[i]};
},
.Fails => {
@compileError(std.fmt.comptimePrint("{} is not a base or composite quality.", .{bases[i].t}));
},
}
}
const sortFunc: fn (void, TypeValuePair, TypeValuePair) bool = struct {
pub fn inner(_: void, a: TypeValuePair, b: TypeValuePair) bool {
return std.mem.order(u8, @typeName(a.t), @typeName(b.t)) == std.math.Order.gt;
}
}.inner;
snippet continued in next message