#Compare two strings at runtime

1 messages · Page 1 of 1 (latest)

mortal grove
#

std.mem.eql is at compile time. == is an ast-check error. Any ideas what I should be doing here?

urban compass
#

what does std.mem.eql is at compile time mean?
it will be forced to run at comptime in a comptime-only context, other than that function calls a always runtime

mortal grove
#

This is the error I get when I use it:

   └─ zig build-exe zfetch Debug native 1 errors
src/main.zig:62:65: error: comptime control flow inside runtime block
            if (std.mem.eql(u8, field.name, "os_name_short")) { continue; }
                                                                ^~~~~~~~
urban compass
#

show the rest of the code

mortal grove
#
    pub fn print(self: Fetch, alloc: std.mem.Allocator) ![]const u8 {
        var lines = std.ArrayList([]const u8).init(alloc);
        defer lines.deinit();

        const header = std.mem.concat(alloc, u8,  &[_][]const u8{self.user, "@", self.host}) catch "unknown";
        const divider_buf = [_]u8{'-'} ** 100;
        const divider = divider_buf[0..header.len - 1];

        try lines.append(header);
        try lines.append("\n");
        try lines.append(divider);
        try lines.append("\n");

        const fields = @typeInfo(Fetch).Struct.fields;
        inline for (fields) |field| {
            if (std.mem.eql(u8, field.name, "os_name_short")) { continue; }
            try lines.append(try std.mem.concat(alloc, u8, &[_][]const u8{colour_string(field.name, alloc), ": ", @field(self, field.name), "\n"}));
        }

        const ret = std.mem.concat(alloc, u8, lines.items) catch "unknown";
        return ret;
    }
};
urban compass
#

inline for does not support continue, you have to use a labeled block with break to emulate continue

inline for (...) |...| blk: {
    if (std.mem.eql(u8, field.name, "os_name_short")) { break :blk; } // emulate `continue` in an `inline for`
    ...
}
#

i probably should have raised an issue about this on the zig repo rather than just letting this sit there

GitHub

There are a few places where we use a trick to get continue behaviour inside an inline for without hitting error: comptime control flow inside runtime block. Why does zig require this? CascadeOS/ke...

mortal grove
#

Ah, thank you -- is it a bug or intentional?