Sometimes I use (comptime optional) void arguments in a function.
For example:
fn get_image(
comptime T: ImageType,
info: if (T == .animation) *const ImageInfo else void) *const Image {
// if animation use info otherwise ignore.
}
and I call it like this for a non-animation:
const img = get_image(.default, VOID);
const img = get_image(.default, {}); // instead of this
to be able to grep search for these kind of callsites I was thinking about a global const:
pub const VOID: void = {};
because searching for {} in an entire project is quite useless 
Any thoughts on this VOID const idea?
Any thoughts on alternatives?
1 Like
I would probably just use a union for this:
const ImageInfo = union(enum) {
default,
animation: *const AnimationInfo,
// other options...
const AnimationInfo = struct {
// ...
};
};
fn get_image(info: ImageInfo) *const Image {
// ...
}
which you would use like:
const img = get_image(.default);
const img = get_image(.{ .animation = &animation_info });
4 Likes
This has runtime costs, while the void approach doesn’t.
2 Likes
A global constant has the issue, that you need to remember to use it and have the friction of importing it.
You could instead use an enum e.g. enum {void}. Note that enums with one element are also 0-bit types, just like void. This would make the callsite take .void which is easier to grep:
const img = get_image(.default, .void);
Another advantage of enums over constants is that you could get autocomplete for it (not sure if it works yet, ZLS is known to have some limitations with comptime code, but it will work eventually).
Also couldn’t you just grep for , {} or , {})? That could narrow it down sufficiently.
2 Likes
Excellent idea! I’ll check that one out.