Skip to content

Instantly share code, notes, and snippets.

@kgn
Last active August 19, 2019 05:40
Show Gist options
  • Save kgn/38334d387ffb61a8c4ebe4a059d2f45f to your computer and use it in GitHub Desktop.
Save kgn/38334d387ffb61a8c4ebe4a059d2f45f to your computer and use it in GitHub Desktop.
@_functionBuilder
struct PeopleBuilder {
static func buildBlock(_ people: Person...) -> [Person] {
return people
}
}
struct Table {
let people: [Person]
// For blocks that return just 1 Person object
init(@PeopleBuilder _ builder: () -> Person) {
self.people = [builder()]
}
// For blocks that return multiple Person objects
init(@PeopleBuilder _ builder: () -> [Person]) {
self.people = builder()
}
}
Table {
Person("Mr. Ed Ford", .chicken)
}
Table {
Person("Mr. John Smith", .beef)
Person("Mrs. Jane Smith", .chicken)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment