You can just do try writer.print("{d}", .{value});
within formatDebug
if T
is a number type. You can just ignore the format options if you want to.
Also you could write your own wrapper around std.debug.print
or whatever you are using and automatically apply that formatting overwrite to any of the tuple arguments which are numbers.
Ok so from @dude_the_builder’s and this statement I realize that I wasn’t clear in my initial question. I have updated it with, hopefully, a clearer intent.
I am trying to debug print arbitrary complex structs which will contain some float fields (among many other fields which might be struct themselves) and I wanted to use decimal notation for those float fields.
For composite types it is more complicated and maybe too much work.
Yes that’s my point. Impossible to do today without a significant effort.
You also could maybe locally hack your std library and just change your default formatting. Maybe you could even copy the standard library formatting code and change it and use it as a custom library, but I don’t know whether it would be relatively straightforward or not.
Hacking it wouldn’t but having to keep it up to date with upstream zig would.
Hmm I guess you are calling it on the inner type, so it should use the default formatting? I am not sure what is going on there. What zig version are you running, do you have a small complete example that creates the compiler bug?
$ zig version
0.12.0-dev.3533+e5d900268
MRE:
const std = @import("std");
fn formatDebugCtr(comptime T: type) (fn (T, []const u8, std.fmt.FormatOptions, anytype) anyerror!void) {
return struct {
fn formatDebug(
any: T,
comptime fmt: []const u8,
options: std.fmt.FormatOptions,
writer: anytype,
) !void {
// TODO: set options to decimal?
try std.fmt.formatType(any, fmt, options, writer, std.options.fmt_max_depth);
}
}.formatDebug;
}
pub fn fmtDebug(any: anytype) std.fmt.Formatter(formatDebugCtr(@TypeOf(any))) {
return .{ .data = any };
}
const MyStruct = struct {
foo: f32,
};
pub fn main() !void {
const myStruct: MyStruct = .{ .foo = 0.123 };
std.log.debug("{}", .{ fmtDebug(myStruct) });
}
compile it with:
$ zig build-exe mre.zig
thread 41603 panic: zig compiler bug: GenericPoison
Unable to dump stack trace: debug info stripped
Aborted (core dumped)