Skip to content

Commit 591c8cd

Browse files
committed
[Function builders] Add an educational note on the build* functions.
Introduce an educational note with a synopsis of the build* function declarations one can add to a function builder, and associate it with the diagnostics indicating incorrect or missing build* functions.
1 parent 51baa57 commit 591c8cd

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

include/swift/AST/EducationalNotes.def

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,15 @@ EDUCATIONAL_NOTES(type_cannot_conform, "protocol-type-non-conformance.md")
8181
EDUCATIONAL_NOTES(unlabeled_trailing_closure_deprecated,
8282
"trailing-closure-matching.md")
8383

84+
EDUCATIONAL_NOTES(function_builder_static_buildblock,
85+
"function-builder-methods.md")
86+
EDUCATIONAL_NOTES(function_builder_missing_limited_availability,
87+
"function-builder-methods.md")
88+
EDUCATIONAL_NOTES(function_builder_missing_build_optional,
89+
"function-builder-methods.md")
90+
EDUCATIONAL_NOTES(function_builder_missing_build_either,
91+
"function-builder-methods.md")
92+
EDUCATIONAL_NOTES(function_builder_missing_build_array,
93+
"function-builder-methods.md")
94+
8495
#undef EDUCATIONAL_NOTES
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Function Builder Methods
2+
3+
To be useful as a function builder, a function builder type must provide a
4+
sufficient subset of function-building methods that enable the transformation of
5+
various statement kinds (`if`, `switch`, `for`..`in`, etc.). The following
6+
example function builder illustrates the various function-building methods one
7+
can define:
8+
9+
```swift
10+
@_functionBuilder
11+
struct ExampleFunctionBuilder {
12+
/// The type of individual statement expressions in the transformed function,
13+
/// which defaults to Component if buildExpression() is not provided.
14+
typealias Expression = ...
15+
16+
/// The type of a partial result, which will be carried through all of the
17+
/// build functions.
18+
typealias Component = ...
19+
20+
/// The type of the final returned result, which defaults to Component if
21+
/// buildFinalResult() is not provided.
22+
typealias Result = ...
23+
24+
/// Required by every function builder to build combined results from
25+
/// statement blocks.
26+
static func buildBlock(_ components: Component...) -> Component { ... }
27+
28+
/// If declared, provides contextual type information for statement
29+
/// expressions to translate them into partial results.
30+
static func buildExpression(_ expression: Expression) -> Component { ... }
31+
32+
/// Enables support for `if` statements that do not have an `else`.
33+
static func buildOptional(_ component: Component?) -> Component { ... }
34+
35+
/// With buildEither(second:), enables support for 'if-else' and 'switch'
36+
/// statements by folding conditional results into a single result.
37+
static func buildEither(first component: Component) -> Component { ... }
38+
39+
/// With buildEither(first:), enables support for 'if-else' and 'switch'
40+
/// statements by folding conditional results into a single result.
41+
static func buildEither(second component: Component) -> Component { ... }
42+
43+
/// Enables support for..in loops in a function builder by combining the
44+
/// results of all iterations into a single result.
45+
static func buildArray(_ components: [Component]) -> Component { ... }
46+
47+
/// If declaration, this will be called on the partial result of an 'if
48+
/// #available' block to allow the function builder to erase type
49+
/// information.
50+
static func buildLimitedAvailability(_ component: Component) -> Component { ... }
51+
52+
/// If declared, this will be called on the partial result from the outermost
53+
/// block statement to produce the final returned result.
54+
static func buildFinalResult(_ component: Component) -> Result { ... }
55+
}
56+
```

0 commit comments

Comments
 (0)