9
1

Why is it x.* but not x.& ?

17d 15h ago by discuss.tchncs.de/u/PotatoesFall in zig@programming.dev

I'm currently learning the language, and I was wondering about this design quirk.

Basically, if we have a variable x which is a pointer, we use x.* to get the value. This is much better than using * as a prefix, since the * might need to be applied to anything in the chain for complex access. I see the usecase.

For example something like

(*(*pointer_to_struct).struct_field_ptr).struct_field

is much more clearly written as

pointer_to_struct.*.struct_field_ptr.*.struct_field

But then it feels really inconsistent that we don't do the same for taking a reference with &. Sure, we only ever take one reference of the entire value, so we don't have the same problem as above, but:

We still have the issue that the & is not next to the value being dereferenced:

&some_ptr.*.some_array_struct_field[5].target

this takes the address of the target struct field, which is on the opposite side of the expression.

It also just feels inconsistent.

Additionally, I think the type declaration for a pointer should be &u8, not *u8. Since the & character is semantically equivalent to "address of", and * is more like "value at address".

Thoughts?

btw it's clear to me that a .* can be omitted when accessing a struct field, but the operation is still there, it's just implicit.