#Defining format fn on tagged union leads to error

1 messages · Page 1 of 1 (latest)

hollow beacon
#

I have a tagged union and define a function format on it which leads to following compile error:

fes > zig test -freference-trace src/parser.zig 
/usr/lib/zig/std/fmt.zig:494:25: error: member function expected 0 argument(s), found 3
        return try value.format(actual_fmt, options, writer);
                   ~~~~~^~~~~~~
src/asm.zig:104:9: note: function declared here
    pub fn format(self: *ASMArgs) []const u8 {
    ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
    formatType__anon_11447: /usr/lib/zig/std/fmt.zig:608:31
    formatType__anon_11393: /usr/lib/zig/std/fmt.zig:615:38
    formatType__anon_11344: /usr/lib/zig/std/fmt.zig:569:39
    formatType__anon_11071: /usr/lib/zig/std/fmt.zig:615:38
    formatType__anon_10977: /usr/lib/zig/std/fmt.zig:641:35
    formatType__anon_10684: /usr/lib/zig/std/fmt.zig:608:31
    formatType__anon_11446: /usr/lib/zig/std/fmt.zig:608:31
    formatType__anon_11392: /usr/lib/zig/std/fmt.zig:615:38
    pub fn format(self: *ASMArgs) []const u8 {

When I rename the function everything works. Is there something built in that handles a defined function named 'format' differently?

quick girder
#

format is used by std.fmt to override how a struct/union/enum displays in print statements

#

In functions like std.debug.print, writer.print, std.log.xxx etc

hollow beacon
#

Ok makes sense, thank you. In other languages I would have guessed to implement an interface or something as I don't use the format function for this specific tagged union anywhere. Are there any other magic methods that lead to compilation errors?

quick girder
#

This isn't really a magic method, just part of std.fmt.format's implementation. You will only run into this issue if you're printing this raw struct somewhere

#

But as far as I know of, there isn't anything else in the std lib designed like this

hollow beacon
#

I was confused because I do not directly print it anywhere

#

I print a struct that has a slice of the tagged union, would this also indirectly print it?

#

/try to resolve the format function on the tagged union?

#

I use 'std.debug.print' there but I assume this uses fmt under the hood