This is how to create a Struct using the FunctionBuilder added in Swift 5.1. It's also active in SwiftUI.
This file contains 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
public struct Company: CustomStringConvertible { | |
let name: String | |
let president: String | |
let numberOfEmployees: Int | |
let adress: String | |
public var description: String { | |
return "name: \(name), president: \(president), numberOfEmployees: \(numberOfEmployees), adress: \(adress)" | |
} | |
} | |
@_functionBuilder public struct CompanyBuilder { | |
public static func buildBlock(_ name: String, _ president: String, _ numberOfEmployees: Int, _ adress: String) -> Company { | |
Company(name: name, president: president, numberOfEmployees: numberOfEmployees, adress: adress) | |
} | |
} | |
@CompanyBuilder | |
func myFunc() -> Company { | |
"株式会社すいすいSwift" | |
"カビゴン小野" | |
1 | |
"神奈川" | |
} | |
let company = myFunc() | |
print(company.description) |