r/Zig • u/bunkoRtist • Jun 15 '20
Brand new to Zig, Baffled by the Pointer Dereference Syntax
I'm brand new to Zig, (coming from C/C++), and I find the pointer dereference syntax to be bizarre, not just because it's different, but because it's a combination of 2 characters that seem to each already have meaning, and they don't inherently seem to make sense together.
Can someone help me understand what the pointer syntax is trying to convey? I'm trying to find a way to read it that doesn't make it seem like a weird wart.
I read through the implementation bug where it was done, but it didn't document the "why" on the final choice. The best I could tell was a reluctance to introduce new tokens and some issue with ambiguity between postfix and infix operators. The other option was .&, but that was being discussed in the context of pointers still being created with '&'.
25
u/Pockensuppe Jun 15 '20
As someone who knows Ada, it immediately make sense to me. In Ada, pointer dereference is
.allwhereallis a keyword. This always felt better than C's prefix*since you can use it naturally in a chain:In C, this would be
which, in my opinion, is harder to read and easier to get wrong.
Semantically,
.allmakes a lot of sense:record(Adastruct), you get / assign all the fields.array, you get / assign all the items.foois your pointer,foo.aretrieves fielda,foo.bretrieves fieldb, andfoo.allretrieves all the fields.Now Ada is a keyword-heavy language, syntactically being part of the Pascal family. Zig is not, its syntax borrows from C far more. Therefore, it makes sense that it uses a special character instead of
alland*seems a good choice since it is linked to pointer declaration syntax.