0

In some languages (C++, Java, Ruby, etc.) an assignment returns a value and can be used in an expression:

x = (y = z);    // ok, 'x' gets the value of 'z'

In other languages (Ada, VHDL), an assignment is a proper statement and cannot be used in as expression:

x := (y := z);   -- error!

However, in both cases, it is possible to find teaching material where the assignment symbol is called the "assignment operator".

My question is, is it technically correct to call the assignment symbol an "operator" in the second case, where it is actually a statement, does not return a value, and cannot be used in the middle of an expression?

  • 2
    define technically correct – gnat May 13 '14 at 17:22
  • @gnat, good point. I thought this meant as specified in a technical document - for instance, in a language reference manual or technical standard. It could also mean a textbook definition of what an operator is. – rick May 13 '14 at 17:35
4

The assignment statement is made up of three parts:

  • The target, or lvalue.
  • The assignment operator.
  • The value to be assigned, or rvalue.

The rvalue can be a constant or an expression that returns a value of the correct (or not necessarily correct, in some languages) type. The requirement is that the value to be assigned itself forms a valid rvalue under the rules of the specific language.

As you point out, some languages make an assignment statement return the assigned value. C, C++, C#, Java, and a whole slew of others do it that way, some with restrictions. This is a potentially dangerous practice, but it is an incredibly useful shorthand notation in the hands of those who know how to use it. As we know, that means people will misuse it, either deliberately or because they don't understand the finer details of the syntax involved. The technical way to express this is that the assignment statement does form a rvalue.

So some other languages make assignment statements valid only as stand-alone statements, and dictate that the assignment operator is only valid inside a valid assignment statement. In other words, in these languages a complete assignment statement is not a valid rvalue.

Both are valid ways to design a language, depending on one's goals. Always keep in mind that C was designed to be able to do basically anything assembly language can do, only in a portable and preferably more readable fashion, and many C-like languages derive many features directly from C, even if the syntax is slightly different. Ada on the other hand was designed to make it extremely hard to write programs that do anything but exactly what is expected.

It follows from the above reasoning that = in C, C++, C#, Java, ...; := in Pascal, Ada, ...; and so on, can all properly be called the assignment operator, which forms one part of a complete assignment statement. Whether a complete assignment statement forms a valid rvalue is a different matter and really has nothing to do with the status of the character sequence as such which is used to indicate assignment to a lvalue.

  • Thanks for the nice background, but what does it take for a symbol to be called an "operator"? I thought it needed to return a value. In your answer, if we replace "assignment operator" with "assignment symbol" everything would match what I expected, but as it is now I don't see a basis to call such symbol an operator. Could you elaborate on that? – rick May 13 '14 at 17:43
  • @rick The difference is whether or not the complete assignment statement forms a valid rvalue. The people who made Ada decided that the assignment statement is not a rvalue (which makes x := (y := z); invalid, because the value side of an assignment by definition must be a rvalue), while the people who designed C decided that an assignment statement is a valid rvalue (making x = (y = z); valid). I've re-read your question and honestly don't see how this (most of which is already in the answer) plus the topmost part of the answer does not answer your question. – a CVn May 13 '14 at 17:57
  • The operator itself forms a part of the statement, just as the binary + (the addition) operator forms one part of a statement. – a CVn May 13 '14 at 17:58
  • @rick Anyway, I have edited my answer to try to make it more clear. Does it make more sense now? – a CVn May 13 '14 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.