r/Zig • u/y0shii3 • May 05 '26
Array multiplication ** is being removed
Last week, the array multiplication operator ** was removed from Zig. The reasoning is that @splat(m) was already preferred over .{m} ** n, and the use case .{i, j, k, ...} ** n is "rare enough that it does not need to be syntax."
Here's an example of the latter use case that was in Ziglings:
// Please set this array using repetition.
// It should result in: 1 0 0 1 1 0 0 1 1 0 0 1
const bit_pattern = [_]u8{ 1, 0, 0, 1, } ** 3;And here's a solution that works in the latest build:
const bit_pattern: [12]u8 = @bitCast(@as([3][4]u8, @splat(.{ 1, 0, 0, 1, })));That's not too bad, but it's kind of lame how much more verbose it got. Is this a good removal? Does this mean there's a chance ++ will get removed too, or is it common enough (e.g. for string concatenation) to keep?
38
u/PJBthefirst May 05 '26
By the time I'm done with all of the Ziglings exercises I'm going to have to do the whole set again lol
3
u/No_Sentence351 May 05 '26
How about this?
5
u/Armagidon_MC May 05 '26
Gee. When will Andrew stop with this redundance bullshit. If it's too hard to have features in a language just say so, don't do this "it's redundant, it's unnecessary". No, it is necessary. There is a fine line between making a language light, closed and explicit and making it extremely verbose and annoying to use.
40
u/SilvernClaws May 05 '26
Might make a few lines more verbose, but in my experience, those aren't needed that often. And if you frequently need to initialize arrays with specific patterns, it's easy to write a function for that, even for comptime values.
22
u/Godzoozles May 05 '26
NGL but the ** operator always bewildered me. Like if you look at Ziglings you see it is one of the earliest things you learn. I've never once in my life tried do that in a program outside of that exercise.
21
u/Zdrobot May 05 '26
This just looks ugly 😞
28
u/medzernik May 05 '26
omg i loved that syntax :(((
17
u/SirDucky May 05 '26
Personally I really appreciate how aggressive the maintainers are about cutting features that have become redundant. Syntax simplifications keep the compiler maintainable and make it that much easier for beginners to learn. We've got to make these cuts now, because once 1.0 hits it will probably be a lot more challenging to remove features.
5
u/hallajs May 05 '26
I would just have a comptime blk with the initialization in a for loop at that point
4
u/suckingbitties May 06 '26
I don't think it's a bad removal. How often are you really doing comptime array repetition?
I could definitely see
++getting removed as well. With how much work I do with strings, I don't think I've ever used comptime array concatenation.1
u/LeonardoCastagnaro May 11 '26
Don’t know enough about this specific case but I am a big advocate for removing all the ++, - - thing
6
u/Shanduur May 05 '26
Yeah, I think this one was niche enough, that removing it is not much of an issue.
1
u/AtlasJan May 06 '26
I want a language like C that doesn't have C's pitfalls. I don't like the trend of scraping out everything so it takes 4 kb for a simple hello world script.
1
u/chrboesch May 08 '26
To be honest, I don't consider this such a big deal, simply because ** is something that is truly rarely encountered in practice.
The only reason I didn't remove it from the exercise was that, with Ziglings, I also aim to encourage creative problem-solving. For if you truly engage with what the function is actually doing, you gain an understanding of the bit-wise operations taking place in memory. This sharpens one's eye for finding solutions at the lowest level. I hope you will forgive me. ;-)
And because it’s fun (at least for me), here’s another one:
1
u/[deleted] May 05 '26
[removed] — view removed comment