[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: conditional expr operator in Octave?
From: |
Jaroslav Hajek |
Subject: |
Re: conditional expr operator in Octave? |
Date: |
Thu, 30 Jul 2009 13:45:36 +0200 |
On Wed, Jul 29, 2009 at 5:27 PM, John W. Eaton<address@hidden> wrote:
> On 29-Jul-2009, Jaroslav Hajek wrote:
>
> | is anyone (beside me) interested in supporting the ternary conditional
> | operator (?:) from C/C++ in Octave?
> | Is there any principal obstacle I don't see?
> | Does anyone (JWE?) have a good idea what would need to be done in
> | lex.l to allow the syntax to be recognized?
> | Although there are workarounds, like
> | cond ? true_val : false_val --> {false_val, true_val}{1+logical(cond))
> | or just use temporary variable + an if block, it seems to me that the
> | ?: operator would improve readability in a number of cases.
>
> I recall trying to add this long ago and giving up because of
> conflicts in the parser (the colon is already used for something else
> involving an expression, so introducings EXP ? EXP : EXP is
> ambiguous. So I think you'll need different syntax. Or, if you try
> to make the colon in ?: have lower precedence than the one : in colon
> expressions, then you will likely introduce some really ugly kluges
> into an already overly complex parser. For example, how would you
> parse
>
> a ? b : c : d : e
>
> ? Even if you can come up with some rules for this, is it worth it?
> The possibility of expressions like this will make code harder to
> understand, not easier.
>
> Finally, I'd also strongly caution against adding new syntax because of
> the possibility of future Matlab compatibility problems.
>
OK, I'm dropping it; instead, I implemented the "merge" function aka
Fortran 90 (which I needed for some time) and slightly extended the
scalar mask case to allow arbitrary (even non-array) values.
http://hg.savannah.gnu.org/hgweb/octave/rev/833109a9f37f
merge (mask, tval, fval)
can now be used instead of mask ? tval : fval (except that it does not
short-circuit).
regards
--
RNDr. Jaroslav Hajek
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz