Skip to content

[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

Merged

Conversation

DougGregor
Copy link
Member

@DougGregor DougGregor commented May 4, 2020

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.

@DougGregor
Copy link
Member Author

@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.
@DougGregor DougGregor force-pushed the function-builders-buildarray branch from 4592a31 to 19e234f Compare May 4, 2020 21:22
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

1 similar comment
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please smoke test macOS

@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci smoke test macOS

1 similar comment
@DougGregor
Copy link
Member Author

@swift-ci smoke test macOS

@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please smoke test Linux

1 similar comment
@DougGregor
Copy link
Member Author

@swift-ci please smoke test Linux

Copy link
Contributor

@xedin xedin left a 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);
Copy link
Contributor

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.

@DougGregor DougGregor merged commit 3760942 into swiftlang:master May 5, 2020
@DougGregor DougGregor deleted the function-builders-buildarray branch May 6, 2020 23:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants