Confusion about error: unable to resolve comptime value

Hi everyone, I am new to Zig and would really some discussion surrounding this issue I am running into to help me learn – thank you!

My program:

const std = @import(“std”);
const print = std.debug.print;

pub fn main() void {
  var count: u32 = 1;
  count += 1;
  const a1: [count]u8 = @splat(‘A’);
  print(“{s}\n”, .{a1});
}

Produces this error:

error: unable to resolve comptime value
const a1: [count]u8 = @splat(‘A’);
note: types must be comptime-known

I wasn’t expecting this because I specified u32 for var count – in that sense, I thought the type is comptime-known. I have learned that a solution to this problem is to define comptime var count = 1, but I don’t understand how that solution contrasts with my current one.

Thank you again for your help :slight_smile:

This is a confusing error message. The problem isn’t types but that count is not const. Arrays are values in zig, and so must have a known size at comptime.

and technically count is not known at comptime because it is not const nor comptime var – is that correct?

Yes.
(Post must be at least 10 character)

2 Likes

the types must be comptime-known, is due to trying to create an array type, whose length is part of the type and therefore must be comptime known. Due to count not being comptime known, the array type is no longer comptime known.

3 Likes