首页 > 解决方案 > 如何在 Bound 是通用的地方扩展 ClosedRange

问题描述

对于一些附加功能,我想扩展ClosedRangeBounds 所在的位置Measurement

extension ClosedRange where Bound == Measurement

…显然不能Measurement作为泛型类型需要<>参数。

extension ClosedRange where Bound == Measurement<UnitType> // Cannot find type 'UnitType' in scope

连接wheres 似乎也不起作用。

我没主意了。

我发现唯一有效的是:

extension ClosedRange where Bound == Measurement<UnitLength> {

    func unit() -> UnitLength {
        return lowerBound.unit
    }

}

let range = Measurement(value: 5, unit: UnitLength.meters)...Measurement(value: 15, unit: UnitLength.meters)
range.unit().symbol // m

但我不想为所有单位类型复制和粘贴它。毕竟这不就是泛型的用途吗?

这接近我想要的,但不起作用:

extension ClosedRange where Bound == Measurement<UnitType> { // Cannot find type 'UnitType' in scope

    func unit() -> UnitType { // Cannot find type 'UnitType' in scope
        return lowerBound.unit
    }

}

let range = Measurement(value: 5, unit: UnitLength.meters)...Measurement(value: 15, unit: UnitLength.meters)
range.unit

编辑:

感谢 Dávid Pásztor 的回答。我想扩展我的问题:

如何为Measurements添加一个UnitType继承自 s的函数Dimension

extension ClosedRange {

    func convertedLowerBound<UnitType>(to unit: UnitType) -> Double where Bound == Measurement<UnitType> {
        lowerBound.converted(to: unit).value // Referencing instance method 'converted(to:)' on 'Measurement' requires that 'UnitType' inherit from 'Dimension'
    }

}

let range = Measurement(value: 5, unit: UnitLength.meters)...Measurement(value: 15, unit: UnitLength.meters)
range.convertedLowerBound(to: .centimeters)
Referencing instance method 'converted(to:)' on 'Measurement' requires that 'UnitType' inherit from 'Dimension'

是的,我可以打电话range.lowerBound.converted(to: .centimeters)。我计划实现的确切功能不是这样工作的。

标签: swiftgenericsrangeextend

解决方案


您需要使扩展本身具有通用性,但是 Swift 尚不支持。不过,您可以使函数本身通用。

extension ClosedRange {
    func unit<UnitType>() -> UnitType where Bound == Measurement<UnitType> {
        lowerBound.unit
    }

    func convertedLowerBound<UnitType: Dimension>(to unit: UnitType) -> Double where Bound == Measurement<UnitType> {
        lowerBound.converted(to: unit).value
    }
}

推荐阅读