Hacker News new | ask | show | jobs
by ggm 1118 days ago
Minor thing. Purely as a side:

in C and C++, the conditional statement is written using if and else, while the conditional expression is written with a ternary operator ? and :.

ending a sentence with . is a must. Including a period directly after a syntactic construct about ? and : is .. jarring because it is possible to attempt to read this as :. as an atomic construct in the language. Or even worse, the three-dots pyramid in some mathematical proof notations.

3 comments

> Also, I always find it interesting the ternary operation is constructed in statements/expressions in ? ... : notation but actually has "two" operators there. the ? is the conditional, the : is the binary-choice. It's a language syntax choice which "side" is true or false and it is also true that you can't construct statements in : without a prior ? but .. its hardly a single operator in syntax terms if it has two disjoint components. := is an unambiguous single operation. condition ? x : y is ... not. It's definedly ambiguous!

In C++, the lexical construct you describe is an atomic operator[0] and neither an expression nor a statement:

  The first operand of the conditional operator is
  evaluated and contextually converted to bool.
  After both the value evaluation and all side effects
  of the first operand are completed, if the result
  was true, the second operand is evaluated. If the
  result was false, the third operand is evaluated. 
0 - https://en.cppreference.com/w/cpp/language/operator_other
I took my comments about ?...: notation out. Basically, at a syntactic level its tempting to regard ? as "the operator" and : as some other operator but really the ternary operation only exists as some hypothetical function in 3 arguments:

  ternary(condition, true-path, false-path)
which executes either true-path or false-path depending on condition. But in notational terms, how "big" condition and true and false parts are is rather unconstrained, so you wind up with the ? and the : widely separated. They aren't operators in the higher sense: they're the syntax which forms the ternary operation as a whole over the expression and it's condition.

Lexing bleeds into syntax and syntax bleeds into semantics.

:. is a possible faux-pas in lexing the sentence around the ?...: construct because of :. being unfortunate.

> I took my comments about ?...: notation out.

I see that now. When I was composing my comment, they were there which is why they were quoted.

My apologies for how the response timing transpired.

> its hardly a single operator in syntax terms if it has two disjoint components

I don't think "operator in syntax terms" is nearly as well-defined as OP thinks. Why couldn't `?` be parsed as half an operator and `:` as the other half? There's no rule that says a parser has to call each distinct symbol token its own "operator". In fact, I'd argue that the only reason this might seem seem like a rule is that almost all of the other operators in common use are either unary or binary, making it easy to use a single token for the operator itself. That's why it's called the "ternary operator"; it's the only one that operates on three things! The only alternative to spreading it out across separate tokens with an operand in between is to put multiple operands in a row on one side of it; as confusing as `foo ? bar : baz` might be, I have strong doubts that `foo ?: bar baz` would be less confusing in most real-world cases.

> Why couldn't `?` be parsed as half an operator and `:` as the other half?

Because the language defines it to be an atomic operator named "conditional operator." As to the implications of that definition, please read the referenced link[0].

> In fact, I'd argue that the only reason this might seem seem like a rule is that almost all of the other operators in common use are either unary or binary, making it easy to use a single token for the operator itself.

It is "a rule" because that is how the language is defined. If you'd like to argue otherwise, please feel free to do so with the C++ standards committee.

0 - https://en.cppreference.com/w/cpp/language/operator_other

From what I can tell, the link doesn't seem to disagree with me? The quote I was responding two characterized `?` and `:` as separate operators, and I argued that they were in fact one operator with two separate syntax tokens. The link you provided describes `?` and `:` as a single operator, which is consistent with what I said. I suspect that the C++ standard doesn't disagree with me that half of two is one, which would mean that `?` is half of the operator, and `:` is the other half.
The other thing I took out is "I get called a fool over beer about this" because nobody seems to find my confusion very compelling. I think my own interpretation of operator in the context of syntax as a single atom is provably false. The problem is, it's burnt into my personal mental lexer over textual representation of code.
In the book, the ":" is typeset in monospace and the "." in proportional font, making their meaning clear.
I'm the author - I think that it's good to signal this kind of thing redundantly, and not rely on the details of typesetting to avoid confusion. I'll create an issue in the repo to rephrase the sentence.
I agree - this is inelegant. I'll make an issue in the repo to rephrase this sentence for the next time I do a round of typo fixes.

Thanks for the feedback!