How to use function builder in Swift 5.1

Khoa Pham
Khoa Pham
Nov 6, 2019 · 1 min read
protocol Task {}

struct Build: Task {}
struct Test: Task {}

@_functionBuilder
public struct TaskBuilder {
public static func buildBlock(_ tasks: Task...) -> [Task] {
tasks
}
}

public func run(@TaskBuilder builder: () -> [Task]) {
MyManager.run(tasks: builder())
}

public func run(@TaskBuilder builder: () -> Task) {
MyManager.run(tasks: [builder()])
}
run {
Build()
Test()
}

Read more

Fantageek

Simple apps that make sense

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade