I'm wondering why there are no operators like +=
, -=
, ++
, -=
, <<=
or x ? y : z
(not an augmented assignment ...) in Ada? Many other languages (C, C++, C#, Java, Perl) have them.
-- Example (C/C++/...):
int a = 3;
a += 4; /* A */
// long: a = a + 4
a++; /* B */
// long: a = a + 1
a = ( a > 3 ? 10 : 5 ); /* C */
// long: ' if a > 3 then a = 10 else a = 5'
-- Example (Ada):
a : integer := 3;
a := a + 4; -- A --
a := a + 1; -- B --
if a > 3 then -- C --
a := 10;
else
a := 5;
end if;
( Example doesn't make sense - only for demonstration )
Is it because ...
:=
and not =
(so +=
-> +=:
)?Because the design of Ada was taken much more closely from mathematics than some other languages... And so...
Assignment is not an operator
Operators have specific properties - they operate on quantities returning a result - while leaving the quantities themselves unchanged.
This is important - stick rigorously to this understanding of an "operator" and you make a lot of optimisations possible because the semantics are much more predictable. Essentially, operators don't have side effects. You can repeat them or factor out repeated ones, and you have a lot more freedom to reorder expressions without changing their results.
If you mistake assignment for an operator, ... well, basically you're screwed. Just ONE "operator" with side effects means you lose valuable properties for ALL operators ... for what? some notational convenience, a hugely fertile breeding ground for bugs, and no extra performance or efficiency.
Incidentally when I had to poke around inside GCC recently I found a function in its expression analyser that explicitly broke (intermediate representation for) a++
and transformed it internally into (intermediate representation for) a = a + 1;
So the shorter form really doesn't appear to be any more efficient!
The same rationale applies (less strictly in Ada than VHDL) to functions - they are just operators in another guise, and pure functions (in VHDL that's every function without the word "impure" in its declaration!) don't have side effects.
Which is also why Ada has both functions and procedures : functions, operators and expressions are essentially similar (and ideally, stateless and side-effect free); procedures, assignments and statements are a separate category (procedure calls and assignments are forms of statement).
Separating the concepts and using the appropriate one for each task goes a long way to making clear programs that you can understand and probably do what you intended...
Oh and Ada-2012 has finally caught up with VHDL-2008 and Algol-W (1963) with if- and case-expressions...
a := (if a > 3 then 10 else 5);
-- to my eyes MUCH more readable than ?: especially in multiple if-exprs!
b := (case a is
when 3 => 5;
when 6|7 => 10;
when others => 0);
It is obvious that the assignments here are still statements...
Just to make sure:
Assignment is not an operator
Ada's designers had an impressive and usually VERY clear grasp of what was possible without compromising integrity and what would just lead to a mess. While newer language features have been added, as compiler techniques developed enough to make them reliable, it has always been a cautious process and the Ada-83 subset is still there virtually intact.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With