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

Hendrik Boom hendrik at topoi.pooq.com
Thu Oct 18 23:04:54 CEST 2012


On Thu, Oct 18, 2012 at 10:28:57AM -0500, Rodney M. Bates wrote:
> 
> 
> On 10/17/2012 07:11 PM, Hendrik Boom wrote:
> >For consistency of notation, IF expressions in Modula 3 should probably
> >have explicit ENDs, as in Algol 68.
> 
> Yes.  There is a somewhat different syntax problem here.
> 
> Would:
>   IF B THEN T ELSE IF C THEN U ELSE IF D THEN V ELSE F
> Mean:
>   IF B THEN T ELSE (IF C THEN U ELSE IF D THEN V ELSE F) (* IF is 
right-associative. *)
> Or:
>   IF B THEN T ELSE IF C THEN U ELSE (IF D THEN V ELSE F) (* IF is left-associative. *)

Can't see how this would happen if IF is left-associative.  It looks 
more ike right-associative to me.  In fact, right-associative should 
look like this:
   IF B THEN T ELSE (IF C THEN U ELSE (IF D THEN V ELSE F)) 

And what could left-associative even mean if IF has no left argument?

> 
> Would:
>   IF B THEN T ELSE E OR F
> Mean:
>   IF B THEN T ELSE (E OR F) (* OR has higher precedence than IF. *)
> Or:
>   (IF B THEN T ELSE E) OR F (* OR has lower precedence than IF. *)
> 
> Precedence and associativity rules for the IF-expression would resolve
> these,

It would.

> and all the existing expression operators have these properties.
> But somehow, the IF-expression intuitively seems to me to be a lot harder
> to follow than for more traditional infix operators.
> 
> And for associativity there is an unusual asymmetry about it.  At first,
> I wanted to write
>   (IF B THEN T ELSE IF C THEN U ELSE) IF D THEN V ELSE F (* IF is left-associative. *)
> But that's really wierd,

Yes, it is, because IF doesn't take a left argument,so it's syntactic 
nonsense.

> and would make
>   IF B THEN T ELSE IF C THEN U ELSE (* Nothing here *)
> an expression, which breaks everything.

Also syntactic nonsense for ane expression.

> 
> Giving it a closing END seems a lot easier to fix the syntax and
> easier to read.

Yup, that's the right answer for IF.  As discovered in Algol 68 long 
ago.  ONly they spelled it FI instead of END.

-- hendrik



More information about the M3devel mailing list