(I’m js now getting into zig / systems programming in general, so sry for any egregious mistakes lol)
I’m writing a brainfsck-to-zig transpiler, and for the ‘.’ command, I wrote this:
// the buffer is defined above with "var buffer: [3]u8 = @splat(undefined);"
std.Io.File.stdout().writer(init.io, &buffer).initInterface(buffer).print("{d}", .{tape.items[pointer]});
However, upon compiling the output from a hello world bf program, I get this error message:
test.zig:110:50: error: no field or member function named 'initInterface' in 'Io.File.Writer'
std.Io.File.stdout().writer(init.io, &buffer).initInterface(buffer).print("{d}", .{tape.items[pointer]});
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
C:\Users\thebo\AppData\Roaming\VSCodium\User\globalStorage\ziglang.vscode-zig\zig\x86_64-windows-0.17.0-dev.864+3deb86baf\lib\std\Io\File\Writer.zig:1:1: note: struct declared here
const Writer = @This();
^~~~~
C:\Users\thebo\AppData\Roaming\VSCodium\User\globalStorage\ziglang.vscode-zig\zig\x86_64-windows-0.17.0-dev.864+3deb86baf\lib\std\Io\File\Writer.zig:67:5: note: 'initInterface' is not a member function
pub fn initInterface(buffer: []u8) Io.Writer {
~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In the zig std docs though, the “initInterface” function is included with std.Io.File.Writer
What’s going on?
std.Io.File.stdout().writer(init.io, &buffer) returns a value of type std.Io.File.Writer. initInterface is a function and not a method, when you call writer().initInterface the compiler assumes there’s a method with that name and it can’t find it, hence the message no member function. You can only access the function by doing std.Io.File.Writer.initInterface. But, that’s unnecessary as std.Io.File.stdout().writer(init.io, &buffer) is the init method you want. The correct usage is
var writer = std.Io.File.stdout().writer(init.io, &buffer); // init the writer
try writer.interface.print("hello :D", .{});
Note the interface field. Due to implementation details you should always use the interface as writer.interface or you can place it into a variable by taking a pointer to it.
const interface = &writer.interface;
Otherwise you’ll have a bad time.
As a side note @splat(undefined) can be simplified to just undefined
For an in depth review check File I/O basics (0.16)
0 Comments
Log in to join the conversation.No comments yet. Be the first to share your thoughts.