Hi All, I’m very very new to Zig (Ruby/JS background and C for my hobby projects). After some searches it seems that the syntax to declare an array of strings is
The reason is that the [_] array size inference syntax is only allowed on an array literal per se. But I share your wonder at why the compiler can’t infer the size when it’s dealing with a compile time known literal value being coerced.
It might be because this var a3: [_][]const u8 = .{ "Hello", "Foo", "Bar" }; tries to assign anonymous struct, aka tuple, to an array variable. I often find that specifying the type in place of that dot at the literal initialization site MyType{} is better than after the colon : MyType =, whenever it can be done.
I believe working your way through the langref gets you a pretty good grasp of the basics. The challenge is to start applying and expanding that knowledge by reasoning in practice about what the compiler does and doesn’t do, what it can and cannot do in specific cases.
In this specific case of initializing an array variable with a tuple while asking the compiler to infer the size of the resulting array, the compiler fails. It certainly seems like it should be possible to infer the size, so my guess was that the current rules of coercing tuples to arrays require you to specify the array size yourself, which in the end is probably a good thing, since it keeps from bloating the compiler with handling all of the special cases like this one and making it more complex (and eventually slower), plus, it forces you to be more conscious and explicit with the type coercion that’s happening in this particular case.
var declares a mutable variable while const declares, well, a constant. A mutable variables must have an explicit type because you can elect to initialize it with undefined like in
var a3: [3][]const u8 = undefined;
in this case it is clear that specific number 3 cannot be replaced with placeholder _.
Constants on the other hand cannot change and its type can be completely inferred from the initizalizer.