This is an automated archive made by the Lemmit Bot.
The original was posted on /r/programminglanguages by /u/betelgeuse_7 on 2023-10-29 19:47:26.
Hello, I hope you all are feeling great.
The syntax for assigning a new value to an element of an array in C-like languages started to seem weird to me. I can’t rationalize it. It seems like an arbitrary exception in the syntax. There may be a logical explanation behind this, I just don’t know.
a = 5
This statement gives a
a new value, which is 5.
To create a “reassignment statement”, we put an identifier on the left-hand side and an expression on the right-hand side.
This makes sense.
But this,
a[0] = 5
is supposed to set the value of the first element of the array called a
to 5.
But the left-hand side is not an identifier. In addition, the syntax a[0]
is meant to retrieve an element from an array. So, in the case of a[0] = 5
, the syntax makes me think; “We are assigning ‘5’ to a value, which is going to be an integer (Almost like 8 = 5
).”.
But if I think of it like; “A variable is just a reference to some memory location, so there is nothing wrong with using a[0]
as the left-hand side, because it is also just a memory location which happens to expect another integer.”, it is totally sensible.
This confusion stems from my presumption of seeing []
s as operators. This is not true for Go, at least.
So, another question arises, should I not make square brackets operators in my language? Because if I do, then these frustrations start to build up. But what are those brackets called, if they are not operators? Just special syntax for every situation?
The “dereference-and-assign” syntax for pointers are the same for me. *a = 42
?
Is *identifier =
a binary operator with two separate parts, somehow?
Thank you.