-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Emit anonymous functions as
constexpr
or mutable
lambdas
See comment thread for previous commit: 4bd0c04
- Loading branch information
Showing
1 changed file
with
7 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
b9e73e0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hsutter I'm wondering what you think about this possibility (https://cpp2.godbolt.org/z/jz5sx6exc):
It's something that was possible before, albeit with the grawlix (#247):
For #741 (comment), I have transformed from the Cpp1 (https://cpp2.godbolt.org/z/evj8PnzE7):
To the Cpp2 (https://cpp2.godbolt.org/z/Yjodo7GdE):
b9e73e0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting.
Re
0$
: Somehow I don't think I thought of capturing a literal. That is useful... but I worry slightly that capturing a literal twice with the meaning that it refers to the same captured variable may be brittle because of being sensitive to misspellings, and that capturing the same literal always means the same shared variable (to capture0$
for two different uses requires naming a variable in scope). But it does seem useful here, though probably less readable than namingfirst
explicitly.Other initial reactions to this code:
int_generator
looks cool (but I want to investigate why the(
)
are needed, likely just a needs a parsing tweak to make it work without those)true$ = false;
was a little jarring at first reading, and took me two readings to get used to, then it didn't seem jarring anymoreif true$ { true$ = false; } else ...
could be an emergent idiom/styleinit$
repeatedly as a captured variable name is interestingThanks for pointing these out!
b9e73e0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I have used something like
0$
and(0)$
for that.I think that's #479, which should be ready to merge.
b9e73e0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On the point of being able to (re)name the capture variable: I suspect using a statement-scope parameter may become an emergent idiom -- at least, it's the first thing I tried!
I think that scares me a little. :) Perhaps the naming idiom reduces the need for that? The only problem with putting it on a declaration like
is that in the current implementation the name
int_generator
is then hidden in an introduced scope and can't be used later in the function.b9e73e0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
And for return values, you need to opt out of the terse syntax, and write
-> _ = { (first := true) return …; }
.As #833 (comment) hints at,
we can already use a statement parameters block for the whole function body.
But that's blocked on #832 (https://cpp2.godbolt.org/z/88MY316qz):
The current syntax does mean that we need still the function body block.
b9e73e0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those should be
inout
to alias the capture.b9e73e0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I translated it too literally (I even left the parentheses in).
I normally use
std::exchange
(https://cpp2.godbolt.org/z/e39ao5rjY):if false$.std::exchange(true) {
.But that ICEs on Clang (llvm/llvm-project#73418) and you know GCC (#746 (comment)).
This
first
idiom is common in cppfront's code base.I have found myself using it, and have put some thought into it.
Ones of my whims has been to introduce a
once_flag
that automatically does thestd::exchange
.But when I consider it from the PoV of cppfront's code base, I feel resistant.
Using it loses symmetry with the places that use
first
again somewhere else in the loop (not my use case, yet).It can also be misused in that same case by using the flag again before the next iteration.
However,
once_flag
works much better now in a function expression (https://cpp2.godbolt.org/z/9E39aq7fs):if (!once_flag()$) {
.Although the previous negatives still apply, and a new one is that it only works so well in a function expression.
It's still a great utility if you only need the
first
idiom in function expressions.The change in model makes me jealous of function expressions.
Now I feel like lending a bigger ear to the proponents of "lambdas everywhere"
(i.e., replacing functions with lambdas at namespace scope).
b9e73e0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the "lambdas without parameters are a little monkey" side of it.
monkey := (:() body);