In my quest to truly embrace Ada, one of the pet peeves of mine is that Ada uses “:=” for variable assignment instead of just “=”. Why is this? Is there an intentional, well-thought-out reason?
I think it is a mathematical convention. =
means “equals” as in comparison and :=
means “equal by definition” which is used for defining things
The Ada 83 rationale doesn’t give a specific reason for using :=
, except that it references ALGOL 60 often, which also used that convention for assignment. I was not able to find a rationale for its use in the ALGOL language.
I think it’s a good idea to reduce confusion between assignment and comparison operators. The allowance of both =
and ==
in C conditionals is a common enough bug that it is documented as CWE-481. While Ada does not allow assignment in expressions, disambiguating these operators at a syntactic level makes intent clearer.
There’s nothing listed in the Ada 83 rationale.
I do know that at least some versions of Smalltalk in the 80’s used to replace :=
with left arrow, but I don’t know when that started.
This article describes how it goes back to Algol 1958: Evolution of the assignment operator – The Craft of Coding
and Wikipedia credits it back to the International Algebraic Language
In symbolic logic, :=
is used as a symbol for “definition”. (For example, Wikipedia lists it on this page.) My impression is that this usage predates that of ALGOL, and the latter took it from the former. However, I can’t remember if I discovered it back when I was perusing some old books / papers on mathematical logic, or through some other circumstance.
There is a nice brochure Safe and Secure Software by John Barnes. It has a dedicated section for this topic, but I suggest to read the whole book.
The proper question is why there are languages that use equality notation for assignment?
Quite a few languages use constructions to the effect of let x = ...
, which is the common convention in mathematics. BASIC used this construction (though the LET
was optional in my TRS-80 Color Computer’s Microsoft BASIC), and Rust uses that now.
I like it! Now you’re appealing to my math and physics side, so you definitely have buy-in from me. I’m a EE, and so I understand the difference between such nuances like “i” for current and “j” for imaginary numbers. The bottom line is making intent clear. Thank you.
I like that. Turn it back on the tyranny of the majority. We’re so used to doing it the “wrong” way that we forgot the original meaning.
Another reason is that in Ada assignment is a statement, in and of itself; linguistically this means that it cannot return a value. Operators (like "+"
, "<"
& "="
), however, do return values. — So, you encounter the same dichotomy distinguishing the two forms of subprograms: Functions, which return a value; and Procedures, which do not.
As an extra to what has been said, some mothern languages have learned that using “=” for assignment is not the best thing. That is why new languages such as Go and Jai use “:=” instead, it is not just Ada!
Other languages don’t use :=
, but neither =
. R uses <-
, which is nicer than =
, but I was surprised to discover that it uses ==
as the equality operator. And that was designed for (and by?) statisticians… Go is the same regarding equality, I think.
S (and therefore also R) also uses =
which is more or less equivalent to <-
but not always…
Here are some links on this topic:
https://renkun.me/2014/01/28/difference-between-assignment-operators-in-r/
…which concludes:
In conclusion, for better readability of R code, I suggest that we only use
<-
for assignment and=
for specifying named parameters.
https://www.reddit.com/r/Rlanguage/comments/47c3z7/comment/d0bw0w3/
…which claims:
Point being: the
=
operator performs assignment, and not just at the top level but everywhere — except when it is syntactically interpretable as a named argument.
Note that both C and S were developed at the Bell Labs in the 1970’s (if you see what I mean ) !..
Why would you use =
for assignment? It’s been the symbol for equality for centuries. It has nothing to do with assignment. Personally, I think the ‘=’ character should not be part of the assignment symbol.
Ada uses :=
for assignment because it’s an Algol-family language, derived from Pascal, which use that as the assignment symbol.
Using =
for assignment leads to the symbol for equality being less intuitive and easy to understand, and results in some common errors that are easily avoidable with another assignment symbol.
You have been conditioned to have this reaction by the B/C gang.
There is a fine article about the assignment operator: Evolution of the assignment operator – The Craft of Coding
APL (and R to some degree) get it best with the “x <-” assignment operator, but “let x=” and “x :=” conventions are not bad either.
Personal opinion. = is used for assignment in some languages because there are more assignments than equality checks in a program. It is just less to type. Having said that, you have a few choices:
- Use = for both assignment and equality check. This means that the use of = is context dependent and there is no in-line assignment. F# does this
- Use = for assignment and use something else for comparison.
Old Fortran uses .EQ.
New Fortran uses ==, .EQ., .EQV.
C family use ==
Javascript/PHP use == and ===
Erlang uses == and =:= - Use = for comparison and something else for assignment.
Ada/Pascal/Algol use :=
APL/R use <-
DIN Algol uses .=
TCL just uses the word SET with no operator. - Spell everything out in English.
COBOL can use the = notation or full blown statements like SUBTRACT A FROM B GIVING C
When you make up a language, however esoteric it is, the syntax needs to be set. Just make a choice and live with the consequences. Python uses = for assignment, := for inline assignment (the Walrus operator) and == for equality checks.