On Wednesday, January 10th, 2024 at 7:34 PM, Jason Merrill via Gcc 
<gcc@gcc.gnu.org> wrote:


> 
> 
> On 1/10/24 16:41, Marek Polacek wrote:
> 
> > On Wed, Jan 10, 2024 at 04:24:42PM -0500, Jason Merrill wrote:
> > 
> > > On 1/10/24 15:59, Marek Polacek wrote:
> > > 
> > > > On Wed, Jan 10, 2024 at 02:58:03PM -0500, Jason Merrill via Gcc wrote:
> > > > 
> > > > > What formatting style do we want for non-trivial lambdas in GCC 
> > > > > sources?
> > > > > I'm thinking the most consistent choice would be
> > > > > 
> > > > > auto l = [&] (parms) // space between ] (
> > > > > { // brace on new line, indented two spaces
> > > > > return stuff;
> > > > > };
> > > > 
> > > > Sure, why not. Consistency is what matters. Thus far we seem
> > > > to have been very inconsistent. ;)
> > > > 
> > > > > By default, recent emacs lines up the { with the previous line, like 
> > > > > an
> > > > > in-class function definition; I talked it into the above indentation 
> > > > > with
> > > > > 
> > > > > (defun lambda-offset (elem)
> > > > > (if (assq 'inline-open c-syntactic-context) '+ 0))
> > > > > (add-to-hook 'c++-mode-hook '(c-set-offset 'inlambda 'lambda-offset))
> > > > > 
> > > > > I think we probably want the same formatting for lambdas in function
> > > > > argument lists, e.g.
> > > > > 
> > > > > algorithm ([] (parms)
> > > > > {
> > > > > return foo;
> > > > > });
> > > > 
> > > > And what about lambdas in conditions:
> > > > 
> > > > if (foo ()
> > > > && [&] (params) mutable
> > > > {
> > > > return 42;
> > > > } ())
> > > > 
> > > > should the { go just below [?
> > > 
> > > I think we don't want the { to go below the [ in general; that was the old
> > > emacs default behavior, and it produced lambda bodies with excessive
> > > indentation.
> > > 
> > > With my adjustment above, emacs indents the { two spaces from the &&, 
> > > which
> > > seems a bit arbitrary but reasonable.
> > 
> > Fair enough, I think that's better.
> > 
> > I suppose we should add a note wrt lambdas to
> > https://gcc.gnu.org/codingconventions.html#Cxx_Conventions
> 
> 
> Indeed, that's my goal.
> 
> Looking through the various lambdas already in the compiler, I see a
> good number already follow my suggested indentation, but a large
> majority omit the space between ] and (, so maybe we don't want it?
> 
> Jason

I'm of the opinion that you should just be naming the lambdas once you
want to use them in complex logic. It gets really messy really quick,
if not immediately. It's not as bad when used in algorithms since they
generally are the last parameter, and it's an established practice to
do so, but it's still not super wonderful when they get long.

The other thing I want to raise is whether we want to allow different
formatting for IILE's, having the opening brace on a new line makes
those use cases look a lot messier imo. IILE's stand out quite a bit
already so the rationale for the current bracing is a little weaker for
lambdas imo, especially IILE's. But for a lambda with parameters I can
see the value of formatting it more strictly.

With that said I understand the value of consistency, so perhaps
consistency is the better way to go.

Alex

Reply via email to