C++ Logo


Advanced search

Re: [std-proposals] "once" keyword

From: Scott Michaud <scott_at_[hidden]>
Date: Wed, 24 May 2023 14:50:41 -0400
It doesn't even need to be immediately invoked. I tend to write lambdas
with a name so that I can collapse the body in my code editor.

A common pattern is a whole pile of return trues with a return false at the
end, or vice-versa, depending on the if statement that I run the lambda in
(whether it's ex: checking if something is out of date, or if ex: any bad
state has occurred).

With the body collapsed, I only need to see one extra line above the if

ex: https://godbolt.org/z/Prohdvf6T

On Wed, May 24, 2023 at 11:14 AM Jason McKesson via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> On Wed, May 24, 2023 at 10:51 AM Arthur O'Dwyer via Std-Proposals
> <std-proposals_at_[hidden]> wrote:
> >
> > On Wed, May 24, 2023 at 10:36 AM LUCE Jean-Sébastien via Std-Proposals <
> std-proposals_at_[hidden]> wrote:
> >>
> >> Yes this was why I talked about another keyword, changing "if"
> behaviour (with break) would create big mess with all existant c++ code.
> >>
> >> I agree with most of our comments, yet I still found "goto" syntax not
> ideal.
> >>
> >> And found it strange to have no "return" equivalent in a sub part of a
> function.
> >
> >
> > You've hit the nail on the head. One of the big ideas of the "structured
> programming" revolution of the '60s–'70s was functional decomposition: the
> idea that if your program is doing more than one "thing," you should break
> it down into smaller functions where each function accomplishes just one
> "thing." Every time you find yourself needing "the equivalent of `return`
> but for a smaller part of a foo," structured programming's answer is always
> going to be: Break that foo down into smaller functions, and then use
> `return`.
> >
> > `return` is the `return` you're looking for. :)
> And with immediately-invoked lambdas, you don't even need to break the
> code out of the function to do that. Sure, the syntax is slightly odd,
> having to prefix the code with `[&]{`, and suffix it with `}();`, but
> overall, it's not much syntax.
> Indeed, there might be a case to be made for having special syntax for
> creating an immediately-invoked lambda expression (always capturing
> `&` and never taking parameters) that puts the syntax at the front of
> the lambda rather than relying on the user to notice the `()` at the
> end.
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2023-05-24 18:50:53