Anonymous structavaganza in Zig

https://news.ycombinator.com/rss Hits: 2
Summary

When statements disappear, what remains of good semantics? Let’s see what side effects have been introduced! To start, observe this truly primordial ‘C code; struct A {}; struct B {}; void example(struct A e); int main(){ example((struct B){}); } clang output: error: passing 'struct B' to parameter of incompatible type 'struct A' example((struct B){}); ^~~~~~~~~~~~ THE TYPES ARE UNIQUE. THEY HAVE DIFFERENT NAMES! THE ARE NOMINALLY DIFFERENT. And such it is for all c structs… But let’s move forward to a distant future far away… or rather close actually. The anonymous, privacy-preserving world of Zig the statementless! Look at this beauty: const A = struct {}; We have bound our capital name to this lost struct! But has the struct recognized our interaction? Or is it like a beautiful immutable atom? Has it changed itself? Let’s take a look: pub fn main() !void { const std = @import("std"); std.debug.print("Who are you? {any}\n", .{A}); } outputs: Who are you? test.A Not only has it given itself the name ‘A’, it even looked outward and saw the file ‘test.zig’. Zig structs have certain nationalistic, or fileistic, tendencies. From this you SHOULD be able to guess how the following behaves: const A = struct {}; const B = A; pub fn main() !void { const std = @import("std"); std.debug.print("Who are you? {any}\n", .{B}); } Yes. The struct does not care for a second name or ‘renaming’. It stays intact: Who are you? test.A The fun part: Struct equality Zig borrows quite a lot from ‘C, like my first example: struct {} == struct {} outputs false. They are different! (There are two LITERAL structs in the source code! They must be different) But yet, oh this can't be! What about the typesystem, who will save it? Everything can’t be different! A great tower of Babel has been built! And soon all variables won’t dare communicate anymore. They all have different types now and all programming is henceforth dead. A looming danger is over us, our pristine generic programming! Poor Array...

First seen: 2025-08-26 17:18

Last seen: 2025-08-26 18:19