#comptime array concatenation via ++

1 messages · Page 1 of 1 (latest)

frozen terrace
#

I thought it was possible to build an array at comptime using array concatenation by doing something like

comptime_array = compime_array ++ .{element};

in a loop, but I can't seem to make it work. Tthe compiler complains (understandably) about a type mismatch as of course the left and right hand sides of that assignment have different type, Is something like this possible? I though I saw an example of this somewhere, but I can't find it. If it's not possible I suppose I will I have to come up with an upper bound for the array size and then do

comptime_array[index] = element;
index += 1;

and then make the array via

const actual_array = array[0..index]
```1
#

comptime array concatenation via ++

torpid radish
#

use slice types

#

for concatenating with the same variable in a loop

frozen terrace
#

I did try that - I get a compile error about it expecting a []T and getting a *const [1]T, I tried massaging this with @as() and @constCast() but haven't been able to make it work

#

It seems to me that @constCast might not be working. I managed to get rid of the error when using slices by making the original comptime slice a []const T, which makes the compiler not report a cast discard const error, but the compiler panics instead with attempt to unwrap error >.<

golden venture
#

What does your code look like to make it panic

pallid horizon
#

comptime var out: []const T = &.{};