[M3devel] in defense of ternary operator (Ada has it now)

Hendrik Boom hendrik at topoi.pooq.com
Thu Oct 18 02:11:47 CEST 2012


On Wed, Oct 17, 2012 at 01:08:01AM +0000, Jay K wrote:
> In defense of ternary operator:Ada 2012 has equivalent of C/C++'s ternary operator:
> e.g.
>   sign := if a >= 0 then 1 else -1 
> They'd rather people NOT do like:
>   sign := ORD(a >= 0) * 1 + ORD(a < 0) * -1 
>  http://www.adacore.com/uploads/technical-papers/Ada2012_Rationale_Chp2_expressions.pdf 
>  "It should be remembered that the difficulties in C stem from a combination of things   - That assignment is permitted as an expression,   - That integer values are used as Booleans,    - That null statements are invisible. 
>   None of these applies to Ada so all is well"    Modula-3 is also safe from the first two, I don't know about the last.  - Jay 		 	   		  

This syntax originated a long time ago, with Algol 60.  It wasn't a 
problem there, even though empty statements were invisible there, too.
These, howerver, are expressions, and there are no empty expressions in 
either Algol 60 or Modula 3.

The only problemm was the dangling "else" as in
  if ... then if ... then ... else ...
But that's only a problem with statements, singe expressions always have 
to have their else's.

For consistency of notation, IF expressions in Modula 3 should probably 
have explicit ENDs, as in Algol 68.

-- hendrik




More information about the M3devel mailing list