Swift 枚举可以将 function/closure 作为原始值吗?
Can a Swift enum have a function/closure as a raw value?
我知道枚举可以有一个闭包作为关联的值,例如:
enum SomeEnum {
case closureOne (String, Double -> Double)
case closureTwo (String, (Double, Double) -> Double)
}
但是,枚举可以作为 raw 值的闭包吗?例如,这样的事情行得通吗?
enum someEnum: () -> Void {
case closureOne = doSomething
case closureTwo = doSomethingElse
}
哪里
let doSomething = {
// Do something here.
}
let doSomethingElse {
// Do something else here.
}
它不是那么简单,但您可以使用 OptionSet
,请参阅 this page:
Unlike enumerations, option sets provide a nonfailable init(rawValue:) initializer to convert from a raw value, because option sets don’t have an enumerated list of all possible cases. Option set values have a one-to-one correspondence with their associated raw values.
可能是这样的:
func doSomething() {}
func doSomethingElse() {}
struct MyClosures: OptionSet {
static let closureOne = MyClosures(rawValue: doSomething)
static let closureTwo = MyClosures(rawValue: doSomethingElse)
let rawValue: () -> Void
init(rawValue: @escaping () -> Void) {
self.rawValue = rawValue
}
init() {
rawValue = {}
}
mutating func formUnion(_ other: __owned MyClosures) {
// whatever makes sense for your case
}
mutating func formIntersection(_ other: MyClosures) {
// whatever makes sense for your case
}
mutating func formSymmetricDifference(_ other: __owned MyClosures) {
// whatever makes sense for your case
}
static func == (lhs: MyClosures, rhs: MyClosures) -> Bool {
// whatever makes sense for your case
return false
}
}
因此您可以将其用作:
let myClosures: MyClosures = [ .closureOne, .closureTwo ]
不过看看你在评论中的解释:
So I'm trying to find the most efficient way to run a function given the state of a variable.
我知道枚举可以有一个闭包作为关联的值,例如:
enum SomeEnum {
case closureOne (String, Double -> Double)
case closureTwo (String, (Double, Double) -> Double)
}
但是,枚举可以作为 raw 值的闭包吗?例如,这样的事情行得通吗?
enum someEnum: () -> Void {
case closureOne = doSomething
case closureTwo = doSomethingElse
}
哪里
let doSomething = {
// Do something here.
}
let doSomethingElse {
// Do something else here.
}
它不是那么简单,但您可以使用 OptionSet
,请参阅 this page:
Unlike enumerations, option sets provide a nonfailable init(rawValue:) initializer to convert from a raw value, because option sets don’t have an enumerated list of all possible cases. Option set values have a one-to-one correspondence with their associated raw values.
可能是这样的:
func doSomething() {}
func doSomethingElse() {}
struct MyClosures: OptionSet {
static let closureOne = MyClosures(rawValue: doSomething)
static let closureTwo = MyClosures(rawValue: doSomethingElse)
let rawValue: () -> Void
init(rawValue: @escaping () -> Void) {
self.rawValue = rawValue
}
init() {
rawValue = {}
}
mutating func formUnion(_ other: __owned MyClosures) {
// whatever makes sense for your case
}
mutating func formIntersection(_ other: MyClosures) {
// whatever makes sense for your case
}
mutating func formSymmetricDifference(_ other: __owned MyClosures) {
// whatever makes sense for your case
}
static func == (lhs: MyClosures, rhs: MyClosures) -> Bool {
// whatever makes sense for your case
return false
}
}
因此您可以将其用作:
let myClosures: MyClosures = [ .closureOne, .closureTwo ]
不过看看你在评论中的解释:
So I'm trying to find the most efficient way to run a function given the state of a variable.