gpt4 book ai didi

swift - 如何在 Swift 5.2 Function Builders 中使用 buildExpression?

转载 作者:搜寻专家 更新时间:2023-11-01 06:52:06 24 4
gpt4 key购买 nike

我知道这是一个 draft proposal .我尝试实现一个简单的 DSL 来构建字符串,如下所示:

@_functionBuilder
struct StringBuilder {
static func buildExpression(_ string: String) -> [String] {
[string]
}
static func buildBlock(_ children: [String]...) -> [String] {
children.flatMap{ $0 }
}
}

func s(separator: String = "", @StringBuilder _ makeString: () -> [String]) -> String {
makeString().joined(separator: separator)
}

let z = s(separator: " ") {
"this"
"is"
"cool"
}

但是,编译器会提示“'String' 无法转换为 '[String]'”。这让我相信 buildBlock 是目前实现的提案的唯一部分。 (这是可以理解的,因为在 SwiftUI 中他们正在构建 View 层次结构,所以这就是他们所需要的。)

这是正确的还是我做错了什么? buildExpression 的正确使用方法是什么?

ielyamani 的回答展示了如何构建一个有效的字符串生成器,就像我在上面的示例中使用的那样。但是,这并不能解决实际问题。我不是要构建字符串生成器。我正在尝试找出函数生成器。字符串生成器只是一个示例。例如,如果我们希望有一个接受整数的字符串构建器,理论上我们可以执行以下操作:

@_functionBuilder
struct StringBuilder {
static func buildExpression(_ int: Int) -> [String] {
["\(int)"]
}

// The rest of it implemented just as above
}

在这种情况下,当编译器遇到 Int 时,它会调用 buildExpression 然后吐出我们的组件类型,在这种情况下 [String]。但正如 Martin R 在对这个问题的评论中所说,buildExpression 当前未实现。

最佳答案

我今天遇到了同样的问题,似乎没有实现 buildExpression。我最终通过使用协议(protocol)“ComponentProtocol”然后创建“Expression:ComponentProtocol”和“Component:ComponentProtocol”来解决问题。现在对我有用。我希望它会在以后实现。

protocol ComponentProtocol: ExpressibleByIntegerLiteral, ExpressibleByStringLiteral  {
var value: String { get }
}

struct Expression: ComponentProtocol {
let _value: String
var value: String { _value }
init(_ value: String) { _value = value }
init(integerLiteral value: Int) { self.init(value) }
init(stringLiteral value: String) { self.init(value) }
init<E: CustomStringConvertible>(_ value: E) {_value = String(describing: value) }
}

struct Component: ComponentProtocol {
let _values: [String]
var value: String { _values.joined(separator: ", ") }
init(integerLiteral value: Int) { self.init(value) }
init(stringLiteral value: String) { self.init(value) }
init<E: CustomStringConvertible>(_ value: E) { _values = [String(describing: value)] }
init<T: ComponentProtocol>(_ values: T...) { _values = values.map { $0.value } }
init<T: ComponentProtocol>(_ values: [T]) { _values = values.map { $0.value } }
}

@_functionBuilder struct StringReduceBuilder {
static func buildBlock<T: ComponentProtocol>(_ components: T ...) -> Component { Component(components) }

static func buildEither<T: ComponentProtocol>(first: T) -> Component { Component(first.value) }
static func buildEither<T: ComponentProtocol>(second: T) -> Component { Component(second.value) }
static func buildOptional<T: ComponentProtocol>(_ component: T?) -> Component? {
component == nil ? nil : Component(component!.value)
}
}

func stringsReduce (@StringReduceBuilder block: () -> Component) -> Component {
return block()
}

let result = stringsReduce {
Expression(3)
"one"
Expression(5)
Expression("2")
83
}

let s2 = stringsReduce {
if .random () { // random value Bool
Expression(11)
} else {
Expression("another one")
}
}

关于swift - 如何在 Swift 5.2 Function Builders 中使用 buildExpression?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56502670/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com