Last active
May 13, 2021 14:36
-
-
Save Alexander-Ignition/e8739956c2f440fd90c8ea6668458433 to your computer and use it in GitHub Desktop.
Simple Swift function builder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @resultBuilder | |
| enum ArrayBuilder { | |
| @inlinable | |
| static func buildExpression<T>(_ expression: T) -> [T] { | |
| [expression] | |
| } | |
| @inlinable | |
| static func buildBlock<T>(_ components: [T]...) -> [T] { | |
| components.flatMap { $0 } | |
| } | |
| @inlinable | |
| static func buildOptional<T>(_ component: [T]?) -> [T] { | |
| component ?? [] | |
| } | |
| @inlinable | |
| static func buildArray<T>(_ components: [[T]]) -> [T] { | |
| components.flatMap { $0 } | |
| } | |
| } | |
| let flag = true | |
| @ArrayBuilder | |
| var numbers: [Int] { | |
| 1 | |
| 2 | |
| if flag { | |
| 3 | |
| 4 | |
| } | |
| for i in 5..<10 { | |
| i | |
| 0 | |
| } | |
| 10 | |
| 11 | |
| } | |
| print(numbers) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment