-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Function builders] Add for...in loop support via buildArray(). #31543
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Function builders] Add for...in loop support via buildArray(). #31543
Conversation
@swift-ci please smoke test |
Extend function builders with support for for..in loops, such as for person in contacts { "Hello \(person.name)" } The loop will be (eagerly) executed and all results will be collected into an array. That array will be passed to a function `buildArray` to produce the result from the loop. Specifically, the above will be translated to the following when used with a function builder type named `FunctionBuilder`,, where all $ names are introduced by the compiler and are not user-visible: let $a1: $T1 var $a2: [$T2] = [] for person in contacts { let $a3: $T3 let $a4 = FunctionBuilder.buildExpression("Hello \(person.name)") $a3 = FunctionBuilder.buildBlock($a4) $a2.append($3) } $a1 = FunctionBuilder.buildArray($a2) where `$a1` is the result of the for-each loop.
4592a31
to
19e234f
Compare
@swift-ci please smoke test |
1 similar comment
@swift-ci please smoke test |
@swift-ci please smoke test macOS |
@swift-ci please smoke test |
@swift-ci smoke test macOS |
1 similar comment
@swift-ci smoke test macOS |
@swift-ci please smoke test |
@swift-ci please smoke test Linux |
1 similar comment
@swift-ci please smoke test Linux |
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.
Nice!
// Form an initialization of the array to an empty array literal. | ||
Expr *arrayInitExpr = ArrayExpr::create(ctx, loc, { }, { }, loc); | ||
cs->setContextualType( | ||
arrayInitExpr, TypeLoc::withoutLoc(arrayType), CTP_CannotFail); |
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.
@CodaFi It seems like this is yet another place where TypeLoc is not really used, usually it's just a Type
or TypeRepr
I think.
Extend function builders with support for for..in loops, such as
The loop will be (eagerly) executed and all results will be collected
into an array. That array will be passed to a function
buildArray
toproduce the result from the loop. Specifically, the above will be
translated to the following when used with a function builder type
named
FunctionBuilder
,, where all $ names are introduced by thecompiler and are not user-visible:
where
$a1
is the result of the for-each loop.