Operator ?:


The ternary conditional operator is sometimes called The Elvis Operator, due to ?: looking similar to his hairstyle when looked sideways.


My advice on implementing the ternary conditional operator

So, you are making a programming language, and you would like to implement the ternary conditional operator ?: in it? Let's not go into discussing how it might make code written in your language less legible, and let's focus only on the strictly technical aspect of it. Here are 5 things I think it's important to be careful about when implementing the ternary conditional operator.
  1. Make sure it's parsed as right-associative.
    Because PHP famously parses it incorrectly as left-associative, preventing it from being used to shortly write if-else-if-else statements. a ? b : c ? d : e should parse as a ? b : (c ? d : e), not as (a ? b : c) ? d : e.
  2. Make sure the code specifying the condition is being executed before the second and the third operand.
    In my AEC-to-x86 compiler, I screwed that one up. Why is it important? Because, sooner or later, some user of your language will try to protect themselves from a divide-by-zero error by writing something like this: d == 0 ? 0 : 1 / d. If d is zero, the code compiled with my AEC-to-x86 compiler will still trigger a division by zero error.
  3. Make sure you output a sensible error message in case somebody puts structures of different types as the second and the third argument.
    Don't output an internal compiler error saying that some part of the compiler attempted to compile an array of negative size, like my AEC-to-WebAssembly compiler used to output, because the user won't be able to guess what's going on from that message.
  4. In case you also support labels with the colon syntax, like C or most dialects of assembly, make sure you correctly determine which colon belongs to a label and which one to a ternary conditional operator.
    A good way to determine that is: if the colon is the second token in a statement, then it is a part of a label, otherwise it's a part of a ternary conditional. If you screw that one up, you will run into problems such as this problem in my PicoBlaze assembler.
  5. In case you are writing an assembler, make sure the preprocessor can deal with labels being put as the second and the third operand.
    Because, sooner or later, somebody will try to use the ternary conditional operator to determine, based on some compile-time condition (say, whether we are in a release or a debugging mode), where the program should jump to. Like this, for example:
    jump NDEBUG ? the_permutations_algorithm : printing_the_sorted_array
    (That's an excerpt from my implementation of the permutations algorithm in PicoBlaze assembly language.). Your assembler must not crash on such a line of code, like mine used to.
I hope this will come useful to somebody. Pieces of advice coming from somebody who studied programming language design and implementation at the university of reality.