#comptime and default value
1 messages · Page 1 of 1 (latest)
comptime fields aren't really normal fields
they're like const declarations
they can only have a single value, which is the one they're initialised with
it's just that instead of accessing them as Type.foo, you access them as value.foo
they're not for budnling comptime state with runtime state
ah, is there a way to generate a type such as @vector(n, u8) where the n is comptime?
what do you mean?
as in, how do you make a generic type?
you'd do that by making a function that returns a type
you can have parameters which are types, or values, or whatever else
e.g.
fn BoundedArray(comptime n: comptime_int) type {
return struct {
arr: [n]u8,
len: usize,
};
}
hmm, that's great. let me try. thank you again
@dim flame I have one more question, if say I have a BoundedArray(1) and a BoundedArray(2), is there a way to put both of them in an arraylist (or something similar) and then be able to loop through them? I even tried MultiArrayList but it can't loop through them easily.
I found a solution without resorting to an arraylist but i'd still love to know. My solution is inline for (comptime 1..3)
inline for (.{ list1, list2 }) |list| {
// do stuff
}
or you take their addresses as .{ &list1, &list2 } if you need to modify them
also, is there a reason you were thinking about an arraylist specifically?
arraylist is kind of a specific concept
if what you're referring to is "something iterable" or a "list" of things, the more general concept is a slice
in this case, you obviously can't use a slice because that also requires all items to be the same type
but you can use a tuple here, which can also be iterated over, with the difference being the loop has to be inline and the length can't be dynamic