The D Programming Language (was: Still more new operators)

Alan J Rosenthal flaps at dgp.toronto.edu
Fri Feb 19 13:24:04 AEST 1988


In article <558 at naucse.UUCP> rrr at naucse.UUCP (Bob Rose ) writes:
>>[How about an operator] that produces its first operand if it is not zero,
>>else it produces its second.

In article <2523 at haddock.ISC.COM> karl at haddock.ima.isc.com (Karl Heuer) writes:
>I considered proposing that the `||' operator be so extended ...
>After thinking about it, though, I decided that this is a step backwards...
>Why should there be a special-purpose notation for `e1 != 0 ? e1 : e2' but
>not for `e1 != -1 ? e1 : e2', say?
>
>What you really want is the `it' pronoun, often used in PDL.
>  IF long-hairy-expression != 0
>    RETURN it
>  ELSE
>    RETURN other-expression
>  ENDIF

An obvious (at least to me) way to extend C to provide this is using the
?: operator.  e1 ? e2 : e3 compiles to something vaguely like:

	calculate e1
	jump-if-zero L1
	calculate e2
	jump-always  L2
    L1: calculate e3
    L2: ...

so, something like "e1 ? : e3" could consistently compile to something like:

	calculate e1
	jump-if-zero L1
	jump-always  L2
    L1: calculate e3
    L2: ...

i.e. just missing the "calculate e2" line, corresponding to the absence
of code in this part of the expression.  (yes yes, the two adjacent
jumps can be reduced to one jump.)

Obviously, this would not break any existing code as "e1 ? : e2" is
currently a syntax error.  And I think this answers Karl Heuer's
question quoted above.

ajr
-- 
"noalias considered sailaon"



More information about the Comp.lang.c mailing list