I try to use comptime to initialise some arrays, roughly:
const width = [_]u8{42, 24};
const height = [_]u8{1, 2};
const sizeBits: [2]u8 = undefined;
const spareBits: [2]u8 = undefined;
comptime {
for (width, height, 0..) |w, h, i| {
sizeBits[i] = w * h;
spareBits[i] = (w * h) % 8;
//...a couple other arrays to calculate at comptime.
}
}
I somehow thought comptime would allow the setting of an undefined const. Something like a static initializer, But:
cannot assign to constant
When using vars I get: unable to evaluate comptime expression and operation is runtime due to this operand, which I think is pointing to the array index.
For sure I can just use a regular fn init() block, but this feels like a perfect use-case for comptime? What am I missing please.
So this morning I learned about tuples, nice! multiple return values was something I was missing from my Go days
I was trying to avoid the messy block syntax with the dodgy goto But in this case it ticks the boxes, comptime, single loop and still maintains const. Thanks!
Edit: I should have tested first… Is this syntax a new feature? Since I get expected ';' after declaration after const sizeBits,