Somewhat new to the language here, so this might be a dumb question, but I’m kinda struggling to find a nice readable way of doing this…
I have some code here that formats 2 comptime ?usize
values (min
and max
) into a string…
const expectedRange =
fmt.comptimePrint("{}...{}", .{ min orelse "", max orelse "" });
For example, I want this to print 1...
if min
and max
are 1 and null.
The issue is that min orelse ""
is either a usize
OR a []const u8
if min
is null… (I think?)
I tried using {?}
as the format specifier and taking out the orelse ""
parts, but that prints null
instead of nothing, and I can’t customize the output for nulls as far as I can tell…
If I leave the brackets empty, it complains that I can’t format an array reference without specifiers, but if I do, {s}
it complains since the possible usize
values aren’t strings. What’s the best way of doing this?
This code here is what I have so far, and does what I want, but there’s gotta be a better way of doing this…
// display-formatted expected range
const expectedRange = makeRangeStr: {
const minD = if (min != null) fmt.comptimePrint("{?}", .{min}) else "";
const maxD = if (max != null) fmt.comptimePrint("{?}", .{max}) else "";
break :makeRangeStr fmt.comptimePrint("{s}...{s}", .{ minD, maxD });
};
Thanks.