Hi.
I was wondering about this. I saw this posting on this site:
http://stackoverflow.com/questions/1642028/what-is-the-name-of-this-operator
where they were mentioning a fake "operator", an artifact of how the C
language is interpreted, that looked like it caused the value of a
variable to "go to" another. Namely, "x --> 0" in the code was just
"x-- > 0", giving it the appearance of having the effect of
"incrementing x toward 0". Now, I was wondering: would this be a good
idea for a new real operator for the C language? Namely, "a -->
b" (and "b <-- a") as real operators that could increment a toward b.
Or would it be pretty worthless?
I can give you five reasons why not.
1. It would be worthless because using it would make your code nonportable to all
the compilers which have decided to ignore implementing such a thing.
The best feature of C is stability, and the best dialect for that is ISO C90,
(with perhaps some very judicious use of new-fangled libraries from C99 and C11.)
2. The --> and <-- syntax has an existing meaning. It is tokenized as {--}{>}
and {<}{--}. The expression a-->b is already valid C. It means (a--) > (b).
In other words, decrement a, and test whether the prior value is greater than b.
So your proposal horribly breaks the current language, making it a complete
nonstarter.
3. Not having b <-- a, you have to write something like a = (a < b) ? a + 1 : a - 1;
This is not a big deal and you can hide it behind a macro, except that it evaluates
a twice:
#define INCTOWARD(A, B) ((A) = (A) < (B) ? (A) + 1 : (A) - 1)
See? You can implement the operator in C already, just not with the syntax you want,
and with a small semantic concession. But this means that the --> operator
does not express anything which cannot be expressed already, it just makes a
small, incremental semantic improvement over the macro. A small, incremental
improvement is harder to justify. Moreover, the syntax that you want breaks
the existing syntax (point 2 above), whereas the INCTOWARD macro doesn't.
4. There is no corresponding machine instruction in any popular machine
instruction set, so you're not going to get better code by providing an inctoward
operator in the language. And even if there is a single machine instruction
for doing (A < B ? A + 1 : A - 1), a compiler just has to recognize this
syntax tree pattern, and check a few constraints and then use the
instruction. It is not strictly necessary to have a dedicated operator.
For instance, some instruction sets have a division instruction which yields both
the quotient and the modulus. Compilers recognize code like
a = b / c; d = b % c and emit just one division instruction from which the
quotient and modulus are obtained for a and d.
5. A trivial new arithmetic operator is not going to lend expressivity to the C language.
And anyway, programmers don't need C to be expressive. By and large they
need it to be a "portable assembler". If you want expressivity, you invent
another programming language and then interpret it with C, or compile it to C.
That language can easily have an A <-- B operator, which expands into
the appropriate C (with clean evaluation semantics and everything).