// *** 在 5.8 版本中 ***
//结果构建器内允许使用任何变量
@resultBuilder
struct TextBuilder {
static func buildBlock(
_ parts: String...) -> String {
parts.joined(separator: " ")
}
}
func makeText(
@TextBuilder _ build: () -> String)
-> String {
build()
}
let text = makeText {
lazy var hello = "Hello,"
let name = "Alina"
hello
name
}
print("text is \"\(text)\"")
//text is "Hello, Alina"
| Lift all limitations on variables in result builders (SE-0373). For example, in Swift 5.8 we can use lazy variables directly inside result builders. |