Newsgroups: comp.lang.c
Path: utzoo!utgpu!jarvis.csri.toronto.edu!dgp.toronto.edu!flaps
From: flaps@dgp.toronto.edu (Alan J Rosenthal)
Subject: Re: The D Programming Language (was: Still more new operators)
Message-ID: <1988Feb18.222404.19539@jarvis.csri.toronto.edu>
Organization: University of Toronto
References: <11702@brl-adm.ARPA> <558@naucse.UUCP> <2523@haddock.ISC.COM>
Date: Thu, 18-Feb-88 22:24:04 EST


In article <558@naucse.UUCP> rrr@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@haddock.ISC.COM> karl@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"
