Files
swift-mirror/lib/Frontend/CompilerInvocation.cpp
Richard Wei b6e679f22c [ResultBuilders] buildBlock(combining:into:) for pairwise combination.
Allow a user-defined `buildBlock(combining:into:)` to combine subexpressions in a block pairwise top to bottom. To use `buildBlock(_combining:into:)`, the user also needs to provide a unary `buildBlock(_:)` as a base case. The feature is being gated under frontend flag `-enable-experimental-pairwise-build-block`.

This will enable use cases in `RegexBuilder` in experimental declarative string processing, where we need to concatenate tuples and conditionally skip captureless regexes.  For example:

```swift
let regex = Regex {
  "a" // Regex<Substring>
  OneOrMore("b").capture() // Regex<(Substring, Substring)>
  "c" // Regex<Substring>
  Optionally("d".capture()) // Regex<(Substring, Substring?)>
} // Regex<Tuple3<Substring, Substring, Substring?>>
let result = "abc".firstMatch(of: regex)
// MatchResult<(Substring, Substring, Substring?)>
```

In this example, patterns `"a"` and `"c"` have no captures, so we need to skip them. However with the existing result builder `buildBlock()` feature that builds a block wholesale from all subexpressions, we had to generate `2^arity` overloads accounting for any occurrences of captureless regexes. There are also other complexities such as having to drop-first from the tuple to obtain the capture type. Though these features could in theory be supported via variadic generics, we feel that allowing result builders to pairwise combine subexpressions in a block is a much simpler and potentially more useful approach.

With `buildBlock(_combining:into:)`, the regex builders can be defined as the following, assuming we have variadic generics:

```swift
enum RegexBuilder {
  static func buildBlock() -> Regex<Substring>
  static func buildBlock<Match>(_ x: Regex<Match>) -> Regex<Match>
  static func buildBlock<
    ExistingWholeMatch, NewWholeMatch, ExistingCaptures..., NewCaptures...
  >(
    _combining next: Regex<(NewWholeMatch, NewCaptures...)>,
    into combined: Regex<(ExistingWholeMatch,  ExistingCaptures...)>
  ) -> Regex<Substring, ExistingCaptures..., NewCaptures...>
}
```

Before we have variadic generics, we can define overloads of `buildBlock(_combining:into:)` for up to a certain arity. These overloads will be much fewer than `2^arity`.
2022-01-18 05:14:08 -08:00

94 KiB