|
| 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