I used to think that #19674 was a prerequisite for #5038. When #5038 was accepted, I thought #19674 would be accepted soon, but to my surprise, it was recently rejected.
Does this mean the appearance of other alternatives?
I used to think that #19674 was a prerequisite for #5038. When #5038 was accepted, I thought #19674 would be accepted soon, but to my surprise, it was recently rejected.
Does this mean the appearance of other alternatives?
Can you provide a little more context about what you’re talking about? I tend to skip posts that don’t make any sense without following a link.
It’s about the following syntaxes
const a: T = .{}; // now mandatory
const a = T{}; // now forbidden
const x = [_]u8{ 1, 2, 3 }; // still allowed
const x: [_]u8 = .{ 1, 2, 3 }; // considered but rejected
I’m wondering if it’s indicative that they might remove the underscore [_] syntax entirely, meaning that neither form will be allowed.
const x = [_]u8{ 1, 2, 3 }; // not allowed
const x: [_]u8 = .{ 1, 2, 3 }; // not allowed
const x = [3]u8{ 1, 2, 3 }; // not allowed
const x: [3]u8 = .{ 1, 2, 3 }; // ok
[_] is almost too convenient and leaves room for lazy programmers to make mistakes; maybe your code indirectly depends on there being three entries, but then one day someone adds a fourth and now your assumptions no longer hold. [3] makes everything more explicit and less surprising because now it’s a compile error if you have too many or too few elements.
I rarely find myself needing to use [_]. If I need a sequence of constant data of a variable or unknown length, a slice &.{} is often the better choice and composes better when nested under multiple levels.
anything to back this claim? any real-life examples?
a: [_]u8 = .{ 1 , 2, 3 }; is kinda redundant when you can just a = .{ 1, 2, 3 };
It does have the nice property of providing a result u8 type to the elements, but you can achieve that with a slice too; and the compiler preserves comptime known slice lengths where possible, so you dont loose that either.
Fair point.
I used to prefer comptime arrays over comptime slices, simply because when I was first learning Zig, using comptime slices always gave me the illusion of returning a stack pointer. After stepping out of my comfort zone, I found that comptime slices can indeed replace the majority of scenarios where inferred-length arrays are used.
Especially note that #35389 allows directly coercing a comptime slice into an array pointer, which is probably the main recent change affecting the disappearance of [_]T.
The motivation for using [_]T is to avoid hardcoding.
Sometimes the list length of ‘3’ is predetermined, so we should avoid changing it by accident, and clearly use [3]T. More often, though, the list length is determined by the contents of the list, and in that case, ‘3’ is a hard-coded value that should be avoided in the source code.
But I agree, currently comptime []T can cover this use case, I had overlooked this before.
[_] syntax is cute, but ultimately it could be a ZLS or even zig fmt feature because it can be inferred by counting the expressions.
I think the main value is about conveying/clarifying intent. Value type vs reference type semantics aside, the main distinction between an array and a slice is that the array’s length is a property of the type, while the slice’s isn’t.
const a: [3]u8 = .{ 1, 2, 3 };
const b: *const [3]u8 = &.{ 1, 2, 3 };
const c: []const u8 = &.{ 1, 2, 3 };
a and b both signal to the reader that the fact that the array length is 3 is significant. For c on the other hand the length probably isn’t as significant, and adding or removing elements is less likely to break assumptions within the program.
To use a reduced real world example from some math code I’ve written, I have two arrays like these:
const sin_coefficients = [_]f64{
0x0p+0,
0x1.87de2ap-2,
0x1.6a09e68p-1,
0x1.d906bdp-1,
0x1p+0,
};
const cos_coefficients = [_]f64{
0x0p+0,
0x1.921d2p-7,
0x1.92156p-6,
0x1.2d8658p-5,
};
Math specifics aside, do you see why a reader might begin to ask questions when reviewing this code? One array has a length of 5, and the other has a length of 4. Was this a mistake on the author’s part? Is there a superfluous element or an element that is missing? Using explicit [5]f64 and [4]f64 would more clearly communicate that this discrepancy is intentional and not a mistake. (In this case the underlying reason for the different lengths has to do with sine and cosine being even and odd functions, respectively.)
I’d advocate for maintaining a consistent syntax, if possible:
mutability name: type = value;
// thus
const a: u8 = 1;
const b: [3]u8 = .{ 1, 2, 3 };
I never understood the existence of [_] since the user can ever only have two intentions, either to specify length or not to, thus [n] and [] should suffice. I think this should go something like:
mutability name: [optional length]type = value;
// thus
const a: []u8 = .{ 1, 2, 3 };
const b: []u8 = .{ 1, 2, 3, 4 };
const c: [3]u8 = .{ 1, 2, 3 };
const d: [4]u8 = .{ 1, 2, 3, 4 };
I think there is value in not specifying length, such as when writing tests, since the number of elements might be changed in quick succession while writing the test (yet never again when done).
Now, while we are at it, why not do:
mutability name: type = value;
// thus
const a: packed struct (u8) = .{ val: u8 };
const b: union(enum) = .{ x: u8, y: u16 };
Finally, I’ll never understand the damn point at the start of type inferred structs, but I’m willing to accept such evil as a tradeoff for having inner scoped enums being suggested by just writing a point. That’s really neat.
Second finally, I just saw that this code:
var a: []u8 = .{ '1', '2', '3', '4' };
Yields:
error: type '[]u8' does not support array initialization syntax
note: inferred array length is specified with an underscore: '[_]u8'
Yet adding what the note says:
var a: [_]u8 = .{ '1', '2', '3', '4' };
Yields:
error: unable to infer array size
Shouldn’t is ask to state the length or use the still allowed = [_]u8{...? ![]()
This was my exact thought after reading this thread. I’ve bounced between liking to (very briefly) not liking, to now feeling indifferent about the inferred type resolution syntax, but having .{} for singular types and then T{} for arrays feels super weird. And to your point about not being able to infer array length, I feel like I can understand and easily intuit most things with Zig at this point, but arrays still give me grief, and that error is one I’ve seen more than once before. Maybe I should just “get good” if it really does make sense (and I imagine the designers have thought about this a lot more than I have) but it’s a situation where the syntax feels completely opposite of what you’d expect
And sometimes you need a sequence of runtime mutable data.
var x = [_]i32{ 1, 2, 3 }; // compiles
const x: []i32 = &.{ 1, 2, 3 }; // doesn't compile
const x: []const i32 = &.{ 1, 2, 3 };
At comptime it is often easier to use const slices.
I find the biggest convenience of the [_]T{} syntax is when you need to concatenate slices or arrays (like for arranging a print format or some building a type at comptime):
Currently, the compiler doesn’t understand what I’m intending here:
test "some nonsense" {
const doingStuff = struct {
fn doingStuff(comptime byte: u8) []const u8 {
// error: expected indexable; found '*const struct { comptime u8 = 16 }'
var str: []const u8 = "this is " ++ &.{byte} ++ " some stuff";
// my lazy ass likes this:
str = "this is " ++ &[_]u8{byte} ++ " some stuff";
// instead of:
str = "this is " ++ @as([]const u8, &.{byte}) ++ " some stuff";
return str;
}
}.doingStuff;
_ = doingStuff(16);
}
I’m always a little put off when I need to use @as() to tell the compiler what my literal means within an expression like this. If concatenation coerces the literal after the ++ operator, that would sure be nice.
There’s a proposal to improve the type inference ability of the ++ operator. But for me, the type inference of operands always feels a bit inconsistent.
It’s a [_]T case, but it’s not a big deal. Thanks to #35389:
const x_init: []const i32 = &.{ 1, 2, 3 };
var x: [x_init.len]i32 = x_init.*;
We can strictly separate the comptime initial value part from the runtime part.
i say leave [_]T, but the compiler prints the size of the array it found for us to input. as with package hashes.
manual counting when you have a GHz machine is stupid. (as is with struct layouts)
you can just
str = "this is " ++ .{byte} ++ " some stuff";
How often do you actually need an initialized sequence of mutable data where the initial values of elements differ? If I need a mutable array, it will almost without exception be in one of two forms:
var x: [n]T = undefined; // explicitly uninitialized
var y: [n]T = @splat(0); // zero/default-initialized
If that compiled, I agree that would be the ideal way to do it.
Someday soon hopefully!