Hello i’m new to zig.
I’m writing a simple toy program that echo back what user input.
const std = @import("std");
// const print = std.debug.print;
pub fn main() !void {
const allocator = std.heap.page_allocator;
const w = std.io.getStdOut().writer();
const r = std.io.getStdOut().reader();
while (true) {
try w.print("> ", .{});
const input = try r.readUntilDelimiterOrEofAlloc(allocator, '\n', 1024);
defer allocator.free(input);
if (input) |s| {
try w.print("said: {s}\n", .{s});
}
}
}
but it doesn’t compile. it says.
/home/linuxbrew/.linuxbrew/Cellar/zig/HEAD-30eb2a1/lib/zig/std/mem/Allocator.zig:488:45: error: access of union field 'Pointer' while field 'Optional' is active
const Slice = @typeInfo(@TypeOf(memory)).Pointer;
~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
/home/linuxbrew/.linuxbrew/Cellar/zig/HEAD-30eb2a1/lib/zig/std/builtin.zig:193:18: note: union declared here
pub const Type = union(enum) {
^~~~~
referenced by:
main: /home/q/Dev/zig/playground/src/main.zig:12:24
callMain: /home/linuxbrew/.linuxbrew/Cellar/zig/HEAD-30eb2a1/lib/zig/std/start.zig:614:32
remaining reference traces hidden; use '-freference-trace' to see all reference traces
line 12 is where i’m freeing the input. so if i don’t free the input the program compiles. what did i do wrong?