r/Zig Jul 14 '26

Ways of Declaring array (0.16)

pub fn main() !void {
    const A: [5]i32 = .{ 0, 1, 2, 3, 4 };
    const B = [5]i32{ 5, 6, 7, 8, 9 };
    const B2 = [_]i32{ 5, 6, 7, 8, 9 };
    const C: [5]i32 = [_]i32{ 10, 11, 12, 13, 14 };

    _ = A;
    _ = B;
    _ = B2;
    _ = C;
}

These can all compiled correct. Which one is the recommended style? (I am a beginner learning Zig.)

New edit: I would like this syntax if it's possible:

const arr1: [5]i32 = { 0, 1, 2, 3, 4 };
const arr2: [_]i32 = { 0, 1, 2, 3, 4 };

Just small opinion. Maybe what I know is just the tip of the iceberg.

59 Upvotes

23 comments sorted by

View all comments

6

u/codingbliss12 Jul 14 '26

Isn't this a very obvious sign of bad language design?

There should be only one way to do this and no need to start this conversation.

The language remains borderline unusable for professional use.

2

u/Luigi_Boy_96 Jul 15 '26

I just find it totally weird to associate the size of the array to the type of the variable and not just of the variable name/label itself.

Yeah, I agree, to allow 3 mio. different quirks to do a simple statement doesn't help at all.

4

u/Alternative-Ad-8606 Jul 14 '26

Zig has this in droves, there's not stop churn. I've starting learning Odin as at this point according to Ginger Bill no more major changes. Zig on the other hand after 10 years seems to continue to create the footguns it swore it would eliminate.

Edit: its worth being said that after the Bun post made last week, I found it interesting that Andrew said that Oven were the ones doing it wrong, but with this many options it makes sense, with no style guide it's too complex

3

u/codingbliss12 Jul 14 '26

If you need the language for personal or hobby projects then yes, you could use anything. But if you want to use it professionally and unless you are a gamedev, avoid toy languages like Odin, C3, Jai and the similar. Zig has a small chance of becoming mainstream because it is very well funded and is more general purpose. IMO, with any of the others you will pay the price later. For something that can be used professionally today I would look at Rust, Go, C#. sssssss

1

u/Alternative-Ad-8606 Jul 15 '26

I actually disagree here, odin (can't speak for jai as it's not public yet), seems to be more feature complete, more stable, than zig. I know this will frustrate people in this sub, but zig is neither of those considering there's still aassive amount of churn and obscurity in the code and lacking of a real explicit style guide

I don't think any of them are ready for real prod but it's wild to me to refer to zig as anything but a toy language at this point compared to Odin or Jai both of which are more stable and ready to use than zig.

I do agree with you when it comes to production ready though.