Skip to content

Commit 30fbdae

Browse files
authored
[SE-289] Adjust selection statements examples to match reality (#1730)
Examples made it look like `buildOptional` calls are injected after enclosing `if` when it doesn't have an else, which is incorrect - `buildOptional` happens inside the block and `else` is synthesized by the result builder transform.
1 parent 138f914 commit 30fbdae

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

proposals/0289-result-builders.md

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -421,9 +421,23 @@ The first transformation pattern for selection statements turns the case into it
421421
```swift
422422
var vCase0: String?
423423
if i == 0 {
424-
vCase0 = "0"
424+
var thenVar = "0"
425+
var thenBlock = BuilderType.buildBlock(thenVar)
426+
vCase0 = BuilderType.buildOptional(.some(thenBlock))
427+
}
428+
```
429+
430+
If `if` statement doesn't have a corresponding `else` block, like in our example, the result builder transform is going create one implicitly and inject a call to `buildOptional(.none)` as follows:
431+
432+
```swift
433+
var vCase0: String?
434+
if i == 0 {
435+
var thenVar = "0"
436+
var thenBlock = BuilderType.buildBlock(thenVar)
437+
vCase0 = BuilderType.buildOptional(.some(thenBlock))
438+
} else {
439+
vCase0 = BuilderType.buildOptional(.none)
425440
}
426-
let v0 = BuilderType.buildOptional(vCase0)
427441
```
428442

429443
The second transformation pattern produces a balanced binary tree of injections into a single partial result in the enclosing block. It supports `if`-`else` and `switch`. Consider the following code:
@@ -443,13 +457,19 @@ Under this pattern, the example code becomes something like the following:
443457
```swift
444458
let vMerged: PartialResult
445459
if i == 0 {
446-
vMerged = BuilderType.buildEither(first: "0")
460+
var firstVar = "0"
461+
var firstBlock = BuilderType.buildBlock(firstVar)
462+
vMerged = BuilderType.buildEither(first: firstBlock)
447463
} else if i == 1 {
464+
var secondVar = "1"
465+
var secondBlock = BuilderType.buildBlock(secondVar)
448466
vMerged = BuilderType.buildEither(second:
449-
BuilderType.buildEither(first: "1"))
467+
BuilderType.buildEither(first: secondBlock))
450468
} else {
469+
var elseVar = generateFibTree(i)
470+
var elseBlock = BuilderType.buildBlock(elseVar)
451471
vMerged = BuilderType.buildEither(second:
452-
BuilderType.buildEither(second: generateFibTree(i)))
472+
BuilderType.buildEither(second: elseBlock))
453473
}
454474
```
455475

0 commit comments

Comments
 (0)