I can’t say I’ve used tuples a lot, and my question is quite newbish: can tuple fields be mutated at all?
For example, this code doesn’t compile:
var a = .{ 1, 2, 3};
a[0] = 10;
tuples.zig:8:10: error: value stored in comptime field does not match the default value of the field
a[0] = 10;
~~~~~^~~~
std.debug.print("{s}\n", .{ @typeName(@TypeOf(a)) });
gives me this (after adding _ = &a;
to make the compiler happy):
struct{comptime comptime_int = 1, comptime comptime_int = 2, comptime comptime_int = 3}
Fields are comptime_int
, and so I can’t modify their values.
Alright, let’s change a
’s initialization to var a = .{ @as(u32, 1), @as(u32, 2), @as(u32, 3) };
Same error:
tuples.zig:8:10: error: value stored in comptime field does not match the default value of the field
a[0] = 10;
~~~~~^~~~
Type of a
is now struct{comptime u32 = 1, comptime u32 = 2, comptime u32 = 3}
.
The reason I’m asking this question is not because I want to mutate tuple fields, but because I’m trying to write a comptime struct populating function, and since tuples are structs, I want to know what should this function do if it gets a tuple, or an anonymous struct like var a = .{ .x = @as(u32, 1), .y = @as(u32, 2), .z = @as(u32, 3) };
where fields are also comptime: struct{comptime x: u32 = 1, comptime y: u32 = 2, comptime z: u32 = 3}
Using @typeInfo(field.type)
on tuple’s fields doesn’t tell me if the field is comptime or not, all I get is that it’s an unsigned 32-bit integer: builtin.Type{ .Int = builtin.Type.Int{ .signedness = builtin.Signedness.unsigned, .bits = 32 } }