[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
formatting ?: operator
From: |
John W. Eaton |
Subject: |
formatting ?: operator |
Date: |
Tue, 25 Apr 2017 01:34:04 -0400 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Icedove/45.6.0 |
While we're on the subject of formatting and whitespace, could we also
choose a convention for the ?: operator when it is longer than one line?
For example, I typically write them like this:
x = ((some_long_condition || some_other_longer_condition)
? some + expression - that / is * not + short
: another + expression - that / is * also + not - short);
By including the extra parens around the whole expression, Emacs knows
how to indent this as above (display with a fixed-width font). I've
noticed that we now have some files that use this convention instead:
retval = lower ? octave::math::gammainc (x, a)
: 1.0 - octave::math::gammainc (x, a);
This looks fine, but I don't know how to get Emacs to do this for me.
I'd rather be able to just hit TAB and have something reasonable happen.
If I ask Emacs to indent the above, I get
retval = lower ? octave::math::gammainc (x, a)
: 1.0 - octave::math::gammainc (x, a);
If I surround the expression with (), then Emacs will indent it as
retval = (lower ? octave::math::gammainc (x, a)
: 1.0 - octave::math::gammainc (x, a));
which is OK, but not really great. By chance, this one lines up neatly,
but that's not generally what will happen. So that's why I would
normally try to get the expression on one line or split before the ? and
possibly the : if both expressions won't fit on one line.
jwe