#error: unable to resolve comptime value

1 messages · Page 1 of 1 (latest)

sleek abyss
#

hi folks! here is the code i'm struggling with https://zigbin.io/129aec

on zig test, it gives:

main.zig:15:76: error: unable to resolve comptime value
pub inline fn initFromSliceEmpty(slice: anytype) StackFromSlice(SliceChild(slice)) {
                                                                           ^~~~~
main.zig:15:76: note: argument to function being called at comptime must be comptime-known
main.zig:88:36: note: called from here
    var stack1 = initFromSliceEmpty(buf[0..]);
                 ~~~~~~~~~~~~~~~~~~^~~~~~~~~~

i'am not sure why it can't get it resolved since all we do here is just types jumbling and they are always available even for the runtime arguments. what am i missing?

celest pier
#

the function initFromSliceEmpty has one argument: slice - slice is runtime known and so cannot be used in calculations of types.

change the helper function SliceChild to accept a type instead of an anytype, and pass in @TypeOf(slice) instead of slice.

sleek abyss
#

hm.. indeed! it worked! thank you super lot!❤️

sleek abyss
#

unfortunately, something went wrong when I integrated this solution and I'm getting type mismatches all around. I thought I understood what I'm doing but it seems I dont :)

Probably I will revert changes to explicit type indication. However, could you explain once again, why the SliceChild with anytype can't get a value and derive type from it to return another type?

#

if feels like this:

fn initFromSliceEmpty(slice: anytype) StackFromSlice(SliceChild(slice)) {...}

initFromSliceEmpty(val);

Can be comptime stepped like this:

1. we know the type of val, so does the initFromSliceEmpty
2. inside initFromSliceEmpty, so does the SliceChild
3. SliceChild gets the type's child and returns it back
4. the type is taken by StackFromSlice, and now we can derive the return type of initFromSliceEmpty
etc.
celest pier
# sleek abyss unfortunately, something went wrong when I integrated this solution and I'm gett...

Zig analyses the code in the following manner:

  • slice is a function argument that isn't marked comptime, so it is runtime known.
  • the call to SliceChild gets as in input slice, which isn't compile-time known, so the function will have to be called at runtime.
  • SliceChild must be evaluated at compile time, as it computes a type - error!
    passing in a type works, because @TypeOf returns a compile-time known value; the function has all of its arguments at compile-time, and so it can be evaluated at compile time, and both because it returns a compile-time-only type (type) and is in a compile-time evaluation context, it must be evaluated at compile-time - so it does.
sleek abyss
#

but isn't anytype forces zig to resolve the type matter first or before it goes into the steps you mentioned?

sleek abyss
#

anyway I still think that no matter whether it is runtime or not, the type information is always available. i can't imagine how it could be different in compile languages like zig

#

so it seems zig does the basic runtime vs comptime args check first without going into details whether the runtime value is actually used just for comptime evulations, right?

celest pier