Should I rather use comptime struct fields or parametric type constant

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 on const value being comptime know.

  • With B Conf is strictly comptime (in my case it is ok/better).

A also clearly shows what is configurable, whereas B relies on reading docs/usage of the config to figure that out

If the standard library is any indication of what is idiomatic, then I’m only aware of Design A within the standard library. One example:

1 Like

Guess I will switch back to A, having things design the same way as in std is a good argument :folded_hands: