I’ve been thinking about the order of tokens from time to time.
In this example we should group right part as *(const T)
.
And sometimes I have some gut feeling that something is wrong here.
I will try to explain.
Let’s start with these simple declarations, in C and in Zig:
const u32 i = 7; // modifier, type, name, initializer
u32 i = 0; // type, name, initializer
const i: u32 = 7; // modifier/qualifier(???), name, type, initializer
var i: u32 = 0; // modifier/qualifier(???), name, type, initializer
Here I’d like to note that I definitely like Zig’s way more, it’s very good, that Zig have not embraced C’s token order. Variable name first, variable type last!
But it seems to me, that const/var
here serve two different purposes:
- denote a name for a memory cell
- denote mutability of the content of that memory cell.
So it’s a kind of confusion of notions. Let’s split them!
val ro i: u32 = 7; // ro = read-only, i.e immutable, constant
val rw i: u32 = 0; // rw = readable-writable, i.e mutable, variable
You could say - “remove this val
keyword and you will get same story”. But wait, here come pointers!
val rw ptr: *ro T; // ptr is a mutable address to constant data.
or may by
val ptr rw: *T ro = &something;
Which would mean “Hey, compiler, listen up! I want memory cell which I want to name ptr
, I want this cell to be changable, treat this cell as a pointer to T
, but do not let me change the pointee”.
What’s your opinion?