Nicely formatted for anyone else reading (to do this, you put three backticks, i.e. ```, before and after the block of text):
abc.zig:19:45: error: unable to resolve comptime value
var fibers = std.ArrayList(*Fiber).init(allocator);
^~~~~~~~~
abc.zig:19:45: note: argument to function being called at comptime must be comptime-known
/data/data/com.termux/files/usr/lib/zig/lib/std/array_list.zig:53:43: note: expression is evaluated at comptime because the function returns a comptime-only type âarray_list.ArrayListAligned(*abc.Fiber,null)â
pub fn init(allocator: Allocator) Self {
^~~~
/data/data/com.termux/files/usr/lib/zig/lib/std/array_list.zig:40:16: note: struct requires comptime because of this field
items: Slice,
^~~~~
abc.zig:5:11: note: struct requires comptime because of this field
func: fn (*Fiber) void, // Function to execute
^~~~~~~~~~~~~~~~
abc.zig:5:11: note: use â*const fn (comptime *abc.Fiber) voidâ for a function pointer type
func: fn (*Fiber) void, // Function to execute
^~~~~~~~~~~~~~~~
The error actually is telling you whatâs wrong! Iâm not faulting you for not understanding it, itâs totally fine to need help with this sort of thing when youâre starting out, but it might help you in future if I walk you through what this error is telling you.
The first bit is telling you that it couldnât resolve allocator
at compile-time. Well, that makes some sense â after all, itâs a runtime value. But the question then becomes, why would it be trying to evaluate it at comptime
? Everything youâre doing here looks like a runtime operation!
Well, thatâs what the error notes are going to tell us. The first note says:
note: argument to function being called at comptime must be comptime-known
Okay, well, thatâs logical, but itâs just moved the question⌠why is the function being called at comptime
? Letâs see if the next note tells us:
note: expression is evaluated at comptime because the function returns a comptime-only type âarray_list.ArrayListAligned(*abc.Fiber,null)â
Right â this note is telling us that itâs because our ArrayList(*Fiber)
type is comptime-only. (By the way, error message, at least at the time of writing, will always print ArrayList(T)
as ArrayListAligned(T, null)
; thatâs just a property of how ArrayList
is implemented. Youâll learn to ignore the âalignedâ bit!)
Well, thatâs moved the question yet again â why would that ArrayList(*Fiber)
type be comptime-only? Letâs read on, perhaps the next note will tell usâŚ
note: struct requires comptime because of this field
items: Slice,
^~~~~
Iâve included the source snippet here, because itâs relevant; items
is the field in ArrayList(*Fiber)
which makes it a comptime-only type. The only way a field can make a struct be comptime-only, is by that field itself being comptime-only. So, Slice
is a comptime-only type. Checking the array_list.zig
source code, on the line it pointed at, we can check the definition of Slice
and see that itâll be []*Fiber
. (This is the one place this error could be possibly improved; this note might be better if it wrote out the type, i.e. struct requires comptime because of field with comptime-only type '[]*Fiber
).
Okay, well, a slice is comptime-only only if its element type is comptime-only, and the same goes for normal pointers; so Fiber
must be comptime-only. Whyâs that? Letâs read on:
abc.zig:5:11: note: struct requires comptime because of this field
func: fn (*Fiber) void, // Function to execute
^~~~~~~~~~~~~~~~
Okay, so fn (*Fiber) void
must be a comptime-only type. Whyâs that?
abc.zig:5:11: note: use â*const fn (comptime *abc.Fiber) voidâ for a function pointer type
func: fn (*Fiber) void, // Function to execute
^~~~~~~~~~~~~~~~
Ah, there we go! We thought that fn (*Fiber) void
was a function pointer, but itâs not; itâs a different type, which is comptime-only. Even if you donât know what a âfunction body typeâ is, the hint is giving you something which sounds like what you want, so you can try it and hopefully it works!