2

I read on Wikipedia that there are often only one ternary operation type possible in C-like languages, which is the conditional expression.

I am trying to find out what other ternary operation exists and what language would make use of them.

B7th
  • 644
  • 1
  • 6
  • 17
  • This is a bit too vague and open ended for SO. – Teepeemm Dec 01 '14 at 18:44
  • Thanks for your comment, unfortunately I don't know where to ask else, if you could direct me that would be sincerely appreciated! – B7th Dec 01 '14 at 18:46
  • Technically, the article doesn't state that it's the only _possible_ ternary operator, but just the only _existing_ ternary operator. – D Stanley Dec 01 '14 at 19:00
  • 2
    To expand a bit on @DStanley - there are actually an infinite number of possible ternary operators. By definition, a ternary operator would be any operator that takes three inputs and produces a single output - no limits on what it actually does with the three inputs, or even the form, domain or range of the inputs or output. C, C++, and many other languages have generally only seen a need to specify the one conditional ternary operator. – twalberg Dec 01 '14 at 19:11
  • 2
    Any theoretical language could be defined to have a ternary operator. For example, let S be the set of all points in the real plane, joined with all triangles in the real plane, joined with the null element. Let T(a,b,c) return the triangle formed by a,b,c, if a,b, and c are all not all colinear (a triangle can not be formed in this case), otherwise the null element. T is a ternary operator. – Zéychin Dec 01 '14 at 21:26

1 Answers1

2
  • Some languages have ternary or even variadic comparison operators, so you can write things like “0 <= x < 15”.
  • I wonder whether things like “<expression> for <variable> in <list>” from python could be considered a ternary operator as well. If you argue that the variable name in there is not an expression, then you could use the “<expresstion> for <variable> in <list> if <condition>” where expression, list and condition are arbitrary expressions. The specification lists generator expressions in the same section as most operators, but doesn't call it an operator and doesn't list it in the table for operator precedence.
  • Many RISC architectures use three operands for common arithmetic operations: two to indicate input values and one to denote the result. Whether you'd call that an operator, and the result register an argument to that operator, is of course again a matter of perspective.

In general, many languages have a far less clear line distinguishing operators from functions on the one side or control constructs on the other side. In those cases it is often hard to decide whether some ternary construct is indeed an operator or not.

MvG
  • 57,380
  • 22
  • 148
  • 276