Updating comptime formatting that used to work more than a year ago

Hi, coming back to trying zig after more than a year, and there are so many changes.

I had this piece of code that would have a helper function to do comptime formatting over some other formatting function. this used to work

fn foo(writer: anytype, bar: []const []const u8) !void {
    // some formatting
    try std.fmt.format(writer, "this {s}", .{bar[0]});
}

pub fn buffComptimeFmt(
    comptime buff_len: usize,
    comptime fmt_func: anytype,
    comptime fmt_func_args: anytype,
) []const u8 {
    comptime {
        // Creates the buffer and allocator
        var buffer: [buff_len]u8 = undefined;
        var stream = std.Io.fixedBufferStream(&buffer);
        @call(
            .compile_time,
            fmt_func,
            // Pass the buffer writer as argument together with needed func arguments
            .{stream.writer()} ++ fmt_func_args,
        ) catch {
            @compileError("No space left in buffer");
        };
        // Return from comptime to runtime
        const written = stream.getWritten();
        const final = written[0..written.len].*;
        return &final;
    }
}

Searching and searching i found what i would imagine would be the substitute with

var stream: std.Io.Writer = .fixed(&buffer);

and just pass stream instead of stream.writer() to @call, but this then fails with

src/lib/utils.zig:81:23: error: unable to evaluate comptime expression
.{stream} ++ fmt_func_args,
^~~~~~
src/lib/utils.zig:81:15: note: operation is runtime due to this operand
.{stream} ++ fmt_func_args,
^~~~~~

so how to make it comptime again?

also its probable the method stream.getWritten()has probably changed names too, but i havent looked into that yet and i will find it when the compilation stops failing with this and inmediatly fails there lol

I don’t think you can use io function at comptime, because IO is for runtime code. You will find similar functionality in std.fmt.comptimePrint I suppose ?

they are not doing io, they are just using a fixed writer.

But yes, std.fmt.comptimePrint probably fits their use case, and is implemented similarly over a fixed writer.

comptime var should fix the error, but you should be passing a ptr.

An important thing to note: the fixed implementation is special for giving an instance of std.Io.Writer directly. Other implementations return their own type that has the interface as a field, and you are required to use it via a pointer to that field due to ptr shenanigans.