kj4tmp
July 28, 2024, 4:57am
1
Are the fields in the following struct guaranteed to initialize in a specific order (will the reader always read dest_mac, src_mac, then ether_type?
const ethernet_header = telegram.EthernetHeader{
.dest_mac = try reader.readInt(u48, big),
.src_mac = try reader.readInt(u48, big),
.ether_type = try reader.readInt(u16, big),
};
1 Like
kj4tmp
July 28, 2024, 5:44am
2
this issue infers that it is well defined.
As far as I can tell, it occurs left-to-right.
opened 10:39AM - 11 Feb 24 UTC
closed 04:25AM - 12 Feb 24 UTC
proposal
zig fmt
I always reorder struct field initialization order to be consistent with the ord… er of the fields in the type. It would be nice if zig fmt could automatically do this so I don't have to.
For instance:
```zig
const Foo = struct { y: u8, a: u8, x: u8 };
const foo = Foo { .a = 42, .x = 69, .y = 32 };
```
would get formatted as:
```zig
const Foo = struct { y: u8, a: u8, x: u8 };
const foo = Foo { .y = 32, .a = 42, .x = 69 };
```
dimdin
July 28, 2024, 6:57am
3
It initializes with the initialization order. But it is not mentioned in the language reference.
A way around this is to have a fn read(reader) !EthernetHeader
method to read in the correct order and construct your header, that works even if zig language specification change.
1 Like