Skip to content

Instantly share code, notes, and snippets.

@ewalk153
Last active March 3, 2022 21:57
Show Gist options
  • Select an option

  • Save ewalk153/10288d22c5e3d489b6d2bf4cbb432284 to your computer and use it in GitHub Desktop.

Select an option

Save ewalk153/10288d22c5e3d489b6d2bf4cbb432284 to your computer and use it in GitHub Desktop.
Exploring how to return a reader with Zig
const std = @import("std");
// fails with compile error
pub fn fileReader() !type {
var file = try std.fs.cwd().openFile("./lines.csv", .{});
defer file.close();
var buf_reader = std.io.bufferedReader(file.reader());
return buf_reader.reader();
}
pub fn main() !void {
_ = fileReader();
}
/usr/local/Cellar/zig/0.9.1/lib/zig/std/os.zig:1456:20: error: unable to evaluate constant expression
const rc = openat_sym(dir_fd, file_path, flags, mode);
^
/usr/local/Cellar/zig/0.9.1/lib/zig/std/fs.zig:1025:27: note: called from here
try os.openatZ(self.fd, sub_path, os_flags, 0);
^
/usr/local/Cellar/zig/0.9.1/lib/zig/std/fs.zig:958:30: note: called from here
return self.openFileZ(&path_c, flags);
^
./file_reader.zig:4:41: note: called from here
var file = try std.fs.cwd().openFile("./lines.csv", .{});
^
./file_reader.zig:11:19: note: called from here
_ = fileReader();
^
./file_reader.zig:10:21: note: called from here
pub fn main() !void {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment