#Construct array at comptime
1 messages · Page 1 of 1 (latest)
for instance:
pub const array: [_]usize = comptime blk: {
//create something i can append to
//and at the end pop the array
};
you can just let the compiler infer the type
pub const array = comptime blk: {
//create something i can append to
//and at the end pop the array
};
the problem is not the result type, it's inside the comptime, i want to iterate and append
so you essentially want a comptime arraylist?
i guess, or just a way to do ++= if you see what i mean
could you place an upper bound on the size?
i guess!
does that solve your issue?
ah, well i guess it's more of a workaround than a solution but yes
so i understand it's not possible
i think you could probably do it with recursion
if u write a working version with the workaround ill give that a try
thats what i was thinking also, i think i'll try the recursion thing myself!
glhf!
Hmmm, IIRC I was regularly using ++ for that. You probably should use a slice variable to store the array tho.
oh, i found a solution for my case but i'd be curious if you have a code snippet because it's not the first time i want to do that
This is with loops. IIRC it gets simpler with recursion, because you can use arrays instead of slices
fn makeArray(comptime n: usize) [n]usize {
var slice: []const usize = &.{};
for (0..n) |i|
slice = slice ++ @as([]const usize, &.{i});
return slice[0..n].*;
}
pub fn main() void {
const a = comptime makeArray(10);
@compileLog(a);
}
The code could probably be cleaned up a bit, this is only to illustrate the idea.
oh what you can just concatenate slices?
has to be comptime only yeah