#Convert enum to custom string values

1 messages · Page 1 of 1 (latest)

glass quartz
#

return type is wrong. should be returning []const u8

viral karma
#

instead of having this function, you could use:
https://ziglang.org/documentation/master/std/#A;std:enums.tagName

also, the problem with your function is, that you're expecting a const pointer to an array be returned, string literals in zig are generally considered to be of type:
[N:0]const u8, which can also be coerced to: [N]const u8 and []const u8.

to fix your problem, to use your own function, you could just:

    pub fn toString(self: TokenType) []const u8 {
        const str = switch (self) {
            .Illegal => "illegal",
            .Eof => "eof",
        };
        return str;
    }
civic oriole
#

okay. i change it to that.

#

using as follows

#
        try stdout.print("Token: {s}\n", tokenType);```
#

get this error

#
        @compileError("expected tuple or struct argument, found " ++ @typeName(ArgsType));
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
    print__anon_3740: /usr/lib/zig/std/io/writer.zig:28:27
    main: zscript.zig:23:19
    remaining reference traces hidden; use '-freference-trace' to see all reference traces```
blissful ferry
#

Print functions take a tuple as second argument

civic oriole
#
        try stdout.print("Token: {s}\n", .{tokenType});```
blissful ferry
#

Pass it as .{tokenType}

#

Ye

civic oriole
#

right.. i see

#

thank you for help