首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用开关控制流从函数推断返回类型

用开关控制流从函数推断返回类型
EN

Stack Overflow用户
提问于 2019-02-23 20:38:16
回答 1查看 59关注 0票数 1

我有一套属性/协议(背景是here,但我认为它是多余的)

类类型如下所示:

代码语言:javascript
复制
struct AdjustmentTypes {
    internal class BaseType<T>: Hashable {

        static func == (lhs: AdjustmentTypes.BaseType<T>, rhs: AdjustmentTypes.BaseType<T>) -> Bool {
            return lhs.name == rhs.name
        }

        typealias A = T

        var hashValue: Int { return name.hashValue }

        let name: String
        let defaultValue: T
        let min: T
        let max: T
        var value: T

        init(name: String, defaultValue: T, min: T, max: T) {
            self.name = name
            self.defaultValue = defaultValue
            self.min = min
            self.max = max
            self.value = defaultValue
        }
    }

    class FloatType: BaseType<CGFloat> { }

    class IntType: BaseType<Int> { }
}

我使用类型擦除来删除类型,这样我就可以将它们存储在Set中,并且我已经构建了一些帮助方法来简化我的Set工具:

代码语言:javascript
复制
class AdjustmentsSet {

    private var adjustmentsSet: Set<AnyHashable> = []

    func insert(_ adjustment: AnyHashable) {
        adjustmentsSet.insert(adjustment)
    }

    func remove(_ adjustment: AnyHashable) {
        adjustmentsSet.remove(adjustment)
    }

    func contains(_ adjustment: AnyHashable) -> Bool {
        return adjustmentsSet.contains(adjustment)
    }

    var count: Int { return adjustmentsSet.count }
}

var adjustmentsSet = AdjustmentsSet()

我现在要做的是向我的Set管理类添加一些帮助程序,以便能够检索具有正确类型的属性,例如,如果我这样做了:

代码语言:javascript
复制
let brightness = Brightness().make()
adjustments.get(brightness)

它应该返回nil,但是如果我返回了:

代码语言:javascript
复制
adjustments.insert(brightness)
adjustments.get(brightness)

现在我应该将值取回,作为它的正确类型AdjustmentTypes.FloatType

我想到了这样的一个Switch语句:

代码语言:javascript
复制
class AdjustmentsSet {

    // ...

    func get(_ adjustment: AnyHashable) -> Any? {
        guard let untyped = adjustmentsSet.first(where: { $0 == adjustment }) else { return nil }
        switch adjustment {
        case _ as AdjustmentTypes.FloatType: return untyped as! AdjustmentTypes.FloatType
        case _ as AdjustmentTypes.IntType: return untyped as! AdjustmentTypes.IntType
        default: return nil
        }
    }
}

但是,当然,致命的缺陷是返回Any,而不是预期的类型。

如何推断返回值的类型并返回正确的类型?

完整的例子,只需把它放在操场上:

代码语言:javascript
复制
// Generic conforming protocol to AnyHashable
protocol AnyAdjustmentProtocol {
    func make() -> AnyHashable
}

protocol AdjustmentProtocol: AnyAdjustmentProtocol {
    associatedtype A
    func make() -> A
}

struct AdjustmentTypes {
    internal class BaseType<T>: Hashable {

        static func == (lhs: AdjustmentTypes.BaseType<T>, rhs: AdjustmentTypes.BaseType<T>) -> Bool {
            return lhs.name == rhs.name
        }

        typealias A = T

        var hashValue: Int { return name.hashValue }

        let name: String
        let defaultValue: T
        let min: T
        let max: T
        var value: T

        init(name: String, defaultValue: T, min: T, max: T) {
            self.name = name
            self.defaultValue = defaultValue
            self.min = min
            self.max = max
            self.value = defaultValue
        }
    }

    class FloatType: BaseType<CGFloat> { }

    class IntType: BaseType<Int> { }
}

struct AnyAdjustmentType<A>: AdjustmentProtocol, Hashable {
    static func == (lhs: AnyAdjustmentType<A>, rhs: AnyAdjustmentType<A>) -> Bool {
        return lhs.hashValue == rhs.hashValue
    }

    private let _make: () -> AnyHashable
    private let hashClosure:() -> Int

    var hashValue: Int {
        return hashClosure()
    }

    init<T: AdjustmentProtocol & Hashable>(_ adjustment: T) where T.A == A {
        _make = adjustment.make
        hashClosure = { return adjustment.hashValue }
    }
    func make() -> AnyHashable {
        return _make()
    }
}

struct Brightness: AdjustmentProtocol, Hashable {
    func make() -> AnyHashable {
        return AdjustmentTypes.FloatType(name: "Brightness", defaultValue: 0, min: 0, max: 1)
    }
}
struct WhiteBalance: AdjustmentProtocol, Hashable {
    func make() -> AnyHashable {
        return AdjustmentTypes.IntType(name: "White Balance", defaultValue: 4000, min: 3000, max: 7000)
    }
}

let brightness = Brightness().make()
let whiteBalance = WhiteBalance().make()

class AdjustmentsSet {

    private var adjustmentsSet: Set<AnyHashable> = []

    func insert(_ adjustment: AnyHashable) {
        adjustmentsSet.insert(adjustment)
    }

    func remove(_ adjustment: AnyHashable) {
        adjustmentsSet.remove(adjustment)
    }

    func contains(_ adjustment: AnyHashable) -> Bool {
        return adjustmentsSet.contains(adjustment)
    }

    var count: Int { return adjustmentsSet.count }
}

var adjustmentsSet = AdjustmentsSet()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-23 21:59:26

您需要将该方法重写为泛型,并使用足够的类型信息调用它,也就是说,您需要预先知道希望该方法返回的类型。

我也不确定AnyHashables是否是理想的。没有什么可以阻止您添加String、Ints和其他可用于调整集的随机类型。

代码语言:javascript
复制
var adjustmentsSet = AdjustmentsSet()
adjustmentsSet.insert("1") // compiles just fine!

或者,您可以使用和传递您的AdjustmentTypes并用泛型方法重写AdjustmentsSet类:

代码语言:javascript
复制
class AdjustmentsSet {

    private var adjustmentsSet: Set<AnyHashable> = []

    func insert<T>(_ adjustment: AdjustmentTypes.BaseType<T>) {
        adjustmentsSet.insert(adjustment)
    }

    func remove<T>(_ adjustment: AdjustmentTypes.BaseType<T>) {
        adjustmentsSet.remove(adjustment)
    }

    func contains<T>(_ adjustment: AdjustmentTypes.BaseType<T>) -> Bool {
        return adjustmentsSet.contains(adjustment)
    }

    func get<T>(_ adjustment: AdjustmentTypes.BaseType<T>) -> AdjustmentTypes.BaseType<T>? {
        return (adjustmentsSet.compactMap { $0 as? AdjustmentTypes.BaseType<T> }).first(where: { $0 == adjustment })
    }

    var count: Int { return adjustmentsSet.count }
}

接下来,make()方法也应该是更强类型的,因为您没有传递AnyHashables。我实现了这样的亮度和白平衡:

代码语言:javascript
复制
extension AdjustmentTypes {
    static let Brightness = AdjustmentTypes.FloatType(name: "Brightness", defaultValue: 0, min: 0, max: 1)
    static let WhiteBalance = AdjustmentTypes.IntType(name: "White Balance", defaultValue: 4000, min: 3000, max: 7000)
}

还利用Swift中的类型别名和结构,使您的调整类型系统具有价值语义:

代码语言:javascript
复制
struct AdjustmentTypes {

    struct BaseType<T>: Hashable {

        static func == (lhs: AdjustmentTypes.BaseType<T>, rhs: AdjustmentTypes.BaseType<T>) -> Bool {
            return lhs.name == rhs.name
        }

        typealias A = T

        var hashValue: Int { return name.hashValue }

        let name: String
        let defaultValue: T
        let min: T
        let max: T
        var value: T

        init(name: String, defaultValue: T, min: T, max: T) {
            self.name = name
            self.defaultValue = defaultValue
            self.min = min
            self.max = max
            self.value = defaultValue
        }
    }

    typealias FloatType = BaseType<CGFloat>
    typealias IntType = BaseType<Int>
}

最后,您可以按预期使用调整集:

代码语言:javascript
复制
var brightness = AdjustmentTypes.Brightness
brightness.value = 0.5

var adjustmentsSet = AdjustmentsSet()
adjustmentsSet.insert(brightness)

let retrievedBrightness = adjustmentsSet.get(AdjustmentTypes.Brightness)! // strongly typed!
retrievedBrightness.value // 0.5
AdjustmentTypes.Brightness.value // 0.0

整个操场:

代码语言:javascript
复制
struct AdjustmentTypes {

    struct BaseType<T>: Hashable {

        static func == (lhs: AdjustmentTypes.BaseType<T>, rhs: AdjustmentTypes.BaseType<T>) -> Bool {
            return lhs.name == rhs.name
        }

        typealias A = T

        var hashValue: Int { return name.hashValue }

        let name: String
        let defaultValue: T
        let min: T
        let max: T
        var value: T

        init(name: String, defaultValue: T, min: T, max: T) {
            self.name = name
            self.defaultValue = defaultValue
            self.min = min
            self.max = max
            self.value = defaultValue
        }
    }

    typealias FloatType = BaseType<CGFloat>
    typealias IntType = BaseType<Int>
}

extension AdjustmentTypes {
    static let Brightness = AdjustmentTypes.FloatType(name: "Brightness", defaultValue: 0, min: 0, max: 1)
    static let WhiteBalance = AdjustmentTypes.IntType(name: "White Balance", defaultValue: 4000, min: 3000, max: 7000)
}

class AdjustmentsSet {

    private var adjustmentsSet: Set<AnyHashable> = []

    func insert<T>(_ adjustment: AdjustmentTypes.BaseType<T>) {
        adjustmentsSet.insert(adjustment)
    }

    func remove<T>(_ adjustment: AdjustmentTypes.BaseType<T>) {
        adjustmentsSet.remove(adjustment)
    }

    func contains<T>(_ adjustment: AdjustmentTypes.BaseType<T>) -> Bool {
        return adjustmentsSet.contains(adjustment)
    }

    func get<T>(_ adjustment: AdjustmentTypes.BaseType<T>) -> AdjustmentTypes.BaseType<T>? {
        return (adjustmentsSet.compactMap { $0 as? AdjustmentTypes.BaseType<T> }).first(where: { $0 == adjustment })
    }

    var count: Int { return adjustmentsSet.count }
}

var brightness = AdjustmentTypes.Brightness
brightness.value = 0.5

var adjustmentsSet = AdjustmentsSet()
adjustmentsSet.insert(brightness)

let retrievedBrightness = adjustmentsSet.get(AdjustmentTypes.Brightness)! // strongly typed!
retrievedBrightness.value // 0.5
AdjustmentTypes.Brightness.value // 0.0

希望这有帮助,祝你的项目好运!

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54845983

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档