help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

conditional expr operator in Octave?


From: John W. Eaton
Subject: conditional expr operator in Octave?
Date: Wed, 29 Jul 2009 11:27:34 -0400

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.

jwe


reply via email to

[Prev in Thread] Current Thread [Next in Thread]