-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Description
Since #24990 stalls on the question of what the right amount of tight capturing is
Idea
I want to propose a headless ->
variant which has the same scoping mechanics as (args...)->
but automatically collects all not-yet-captured underscores into an argument list. EDIT: Nesting will follow the same rules as variable shadowing, that is, the underscore binds to the tightest headless ->
it can find.
Before | After |
---|---|
lfold((x,y)->x+2y, A) |
lfold(->_+2_,A) |
lfold((x,y)->sin(x)-cos(y), A) |
lfold(->sin(_)-cos(_), A) |
map(x->5x+2, A) |
map(->5_+2,A) |
map(x->f(x.a), A) |
map(->f(_.a),A) |
Advantage(s)
In small anonymous functions underscores as variables can increase the readability since they stand out a lot more than ordinary letters. For multiple argument cases like anonymous functions for reduce/lfold it can even save a decent amount of characters.
Overall it reads very intuitively as start here and whatever arguments you get, just drop them into the slots from left to right
-> ---| -----|
V V
lfold(->sin(_)-cos(_), A)
Sure, some more complex options like reordering ((x,y)->(y,x)
), ellipsing ((x...)->x
) and probably some other cases won't be possible but if everything would be possible in the headless variant we wouldn't have introduced the head in the first place.
Feasibility
- Both, a leading
->
and an_
as the right hand side (value side) error on 1.5 so that shouldn't be breaking. - Since it uses the well-defined scoping of the ordinary anonymous functions it should be easy to
2a) switch between both variants mentally
2b) reuse most of the current parser code and just extend it to collect/replace underscores
Compatibility with #24990
It shouldn't clash with the result of #24990 because that focuses more on tight single argument very tight argument cases. And even if you are in a situation where the headless ->
consumes an underscore from #24990 unintentionally, it's enough to just put 2 more characters (->
) in the right place to make that underscore once again standalone.
Further Explorations
This proposal can optionally be combined with #53946.
Additionally, the following links to comments further down explore different ideas to stretch into, all adding their own value to different parts of the ecosystem.
Alternative explorations: #38713 (comment) #38713 (comment)