On a personal project I experience slow compile likely related to my typing being a tiny bit too complex.
While refactoring I was wondering what would be the best or more idiomatic choice between these two designs?
(I recently switch from A to B but still not too sure).
Design A
fn Mytype(comptime C: MytypeConf) type {
return struct {
pub const C = _C;
....
};
}
const MytypeConf = struct {
max: comptime_int,
other: comptime_int,
};
const MyConf: MytypeConf = .{
.max = max_calc(),
.other = other_calc(),
};
const MyActualType = Mytype(MyConf);
const access_max = MyActualType.C.max;
Design B
fn Mytype(_C: type) type {
return struct {
pub const C = _C;
....
};
}
const MyConf = struct {
pub const max: comptime_int = max_calc();
pub const other = other_calc()
};
const MyActualType = Mytype(MyConf);
const access_max = MyActualType.C.max;
My guesses
-
A allows better zls integration.
-
Not sure if A would go compiling/resolving
other_calc
if .max is accessed (due to resolving C to acces max, not strictly necessary but may be implemented this way). B would not. -
B would accept pretty much anything (usually I type check through a constant marker here)
-
B do not have to use
comptime
keyword and just rely onconst
value being comptime know. -
With B Conf is strictly comptime (in my case it is ok/better).