• ternary operator associativity (was Re: this girl calls c ugly)

    From Kragen Javier Sitaker@3:633/10 to All on Wed Sep 2 00:30:48 2026
    Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
    I vaguely recall that there's some language that uses the ?: syntax
    for the conditional operator, but with a different precedence and/or associativity than C. I can't remember which language it is.

    PHP were the ones that screwed it up, as usual. ?: is supposed to left-associate, so that you can say things like

    return x < y ? LESS_THAN
    : x > y ? GREATER_THAN
    : EQUAL;

    The second conditional is nested inside the else-clause of the first
    one, and you can make the chain of conditionals of any length you like
    without impairing readability (assuming that you find ?: readable in the
    first place).

    But PHP was written by people who didn't know how to program, so in PHP,
    it right-associates, so that the above would parse as


    return (x < y ? LESS_THAN : x > y) ? GREATER_THAN
    : EQUAL;

    This is not useful.

    PHP having been written by non-programmers had many great advantages;
    for example, they were able to design a programming language that non-programmers could use productively, in part because their
    documentation was excellent. But it did have serious drawbacks, too.

    Kragen

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)