#unable to evaluate comptime expression

1 messages · Page 1 of 1 (latest)

vestal raven
#

So im working on acent of code 2023 day 10 and im getting the following error:

src/2023/10.zig:69:42: error: unable to evaluate comptime expression
                        var n = *tiles[j - y];
                                       ~~^~~
src/2023/10.zig:69:40: note: operation is runtime due to this operand
                        var n = *tiles[j - y];
test "p1_1" {
    ...
    var buffer: []const u8 =
        \\.....
        \\.S-7.
        \\.|.|.
        \\.L-J.
        \\.....
    ;
    const maze = try PipeMaze().init(allocator, buffer);
    ...
}

fn PipeMaze() type {
    return struct {
        ...
        fn init(allocator: std.mem.Allocator, buffer: []const u8) !Self {
            // get dimensions
            const x = std.mem.indexOf(u8, buffer, "\n") orelse unreachable;
            const y = std.mem.count(u8, buffer, "\n") + 1;
            ...
            
            // second pass
            for (tiles, 0..) |tile, j| {
                switch (tile.tile_type) {
                    TileType.ns => {
                        var n = *tiles[j - y]; // <= here
                        var s = *tiles[j + y];
                        ...
                }
                ...
            }
            ...
        }
        ...
    }
}

comptime keyword is not used in this file so theres some implicit mechanism im unaware of. I can see how the test buffer and its dimensions could be comptime implicitly. That said, how can I let the compiler understand that I want the dimension y available during runtime?

grave shoal
#

*T is an expresion for a pointer to type T, dereference is x.*

vestal raven
# grave shoal `*T` is an expresion for a pointer to type `T`, dereference is `x.*`

Thats embarassing, yes that would be correct. I had to get access to a non const version (and could cut out two lines) so I ended up doing:

// second pass
for (0..tiles.len) |j| {
    var tile = &tiles[j];
    switch (tile.tile_type) {
        TileType.ns => {
            tile.n = &tiles[j - y];
            tile.s = &tiles[j + y];
            tile.n.?.s = tile;
            tile.s.?.n = tile;
        },
        ...
sleek wyvern
#

|*tile| makes tile a *T