# I can't search data in table with \`functable\[i\].func()\` because of comptime

**URL:** <https://ziggit.dev/t/i-cant-search-data-in-table-with-functable-i-func-because-of-comptime/3692>\
**Category:** Help\
**Tags:** language\
**Created:** [March 25, 2024, 4:05pm UTC](https://ziggit.dev/t/i-cant-search-data-in-table-with-functable-i-func-because-of-comptime/3692 "2024-03-25T16:05:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MickeyOh](https://ziggit.dev/user_avatar/ziggit.dev/mickeyoh/32/1574_2.png) [@MickeyOh](https://ziggit.dev/u/MickeyOh)\
**Post date:** [March 25, 2024, 4:05pm UTC](https://ziggit.dev/t/i-cant-search-data-in-table-with-functable-i-func-because-of-comptime/3692/1 "2024-03-25T16:05:18Z")

</div>

I am having an issue with the following codes.

```zig
fn func01() void {
    print("func01\n", .{});
}
fn func02() void {
    print("func02\n", .{});
}
const FuncT = struct { no: u32, func: fn () void };
const functable = [_]FuncT{ FuncT{ .no = 1, .func = func01 }, FuncT{ .no = 2, .func = func02 } };

pub fn main() !void {
    var len = functable.len;
    for (0..len) |i| {
        functable[i].func();
    }
}

```

error code:

```zig
src/main.zig:18:19: error: values of type '[2]main.FuncT' must be comptime-known, but index value is runtime-known
        functable[i].func();

```

I don’t want to avoid using comptime as I can comply the codes as they are.

How to handle this issue?

---

<div class="post-metadata">

**Author:** ![Sze](https://ziggit.dev/user_avatar/ziggit.dev/sze/32/496_2.png) [@Sze](https://ziggit.dev/u/Sze)\
**Post date:** [March 25, 2024, 4:11pm UTC](https://ziggit.dev/t/i-cant-search-data-in-table-with-functable-i-func-because-of-comptime/3692/2 "2024-03-25T16:11:04Z")

</div>

You can use [inline for](https://ziglang.org/documentation/master/#inline-for)

> For loops can be inlined. This causes the loop to be unrolled, which allows the code to do some things which only work at compile time, such as use types as first class values. The capture value and iterator value of inlined for loops are compile-time known.

```zig
inline for (0..len) |i| {
    functable[i].func();
}

```

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [March 25, 2024, 4:24pm UTC](https://ziggit.dev/t/i-cant-search-data-in-table-with-functable-i-func-because-of-comptime/3692/3 "2024-03-25T16:24:11Z")

</div>

@sze answer is perfect for your needs, but I would like to highlight 3 points.

```zig
const std = @import("std");
const print = std.debug.print;

fn func01() void {
    print("func01\n", .{});
}
fn func02() void {
    print("func02\n", .{});
}
const FuncT = struct { no: u32, func: *const fn () void };
const functable = [_]FuncT{
    FuncT{ .no = 1, .func = func01 },
    FuncT{ .no = 2, .func = func02 },
};

pub fn main() !void {
    for (functable) |funct| {
        funct.func();
    }
}

```

1. `zig fmt` formats horizontally if a comma is missing from the last element. I added a comma after the last `FuncT` in `functable`. If there is an extra comma after the last element `zig fmt` formats vertically.
2. `for` statement accepts an array, you don’t need an index.
3. Functions are for compile time only, function pointers can be used in runtime by adding a `*const ` before `fn`.

---

<div class="post-metadata">

**Author:** ![MickeyOh](https://ziggit.dev/user_avatar/ziggit.dev/mickeyoh/32/1574_2.png) [@MickeyOh](https://ziggit.dev/u/MickeyOh)\
**Post date:** [March 25, 2024, 9:01pm UTC](https://ziggit.dev/t/i-cant-search-data-in-table-with-functable-i-func-because-of-comptime/3692/4 "2024-03-25T21:01:05Z")

</div>

@sze  
Thanks for quick reply,  
However, it didn’t work out as I tried `inline` before I posted.  
error says

```zig
src/main.zig:15:17: error: unable to resolve comptime value
    inline for (0..len) |i| {
                ^---------

```

my version: 0.11.0

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [March 25, 2024, 9:05pm UTC](https://ziggit.dev/t/i-cant-search-data-in-table-with-functable-i-func-because-of-comptime/3692/5 "2024-03-25T21:05:29Z")

</div>

I’ am getting a much better error message:

```zig
test.zig:14:9: error: local variable is never mutated
    var len = functable.len;
        ^~~
test.zig:14:9: note: consider using 'const'

```

Changing to `const` it works.

```zig
const std = @import("std");
const print = std.debug.print;

fn func01() void {
    print("func01\n", .{});
}
fn func02() void {
    print("func02\n", .{});
}
const FuncT = struct { no: u32, func: fn () void };
const functable = [_]FuncT{ FuncT{ .no = 1, .func = func01 }, FuncT{ .no = 2, .func = func02 } };

pub fn main() !void {
    const len = functable.len;
    inline for (0..len) |i| {
        functable[i].func();
    }
}

```

---

<div class="post-metadata">

**Author:** ![MickeyOh](https://ziggit.dev/user_avatar/ziggit.dev/mickeyoh/32/1574_2.png) [@MickeyOh](https://ziggit.dev/u/MickeyOh)\
**Post date:** [March 26, 2024, 4:33am UTC](https://ziggit.dev/t/i-cant-search-data-in-table-with-functable-i-func-because-of-comptime/3692/6 "2024-03-26T04:33:23Z")

</div>

Thanks a lot for leading me to resolve this.

Thanks as well for the following info. this resolves what I had been annoying.

> 1. `zig fmt` formats horizontally if a comma is missing from the last element. I added a comma after the last `FuncT` in `functable`. If there is an extra comma after the last element `zig fmt` formats vertically.
