如何扩展 Bound 是通用的 ClosedRange

How to extend ClosedRange where Bound is generic

对于一些额外的功能,我想扩展 ClosedRange,其中边界是 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 的回答。我想扩展我的问题:

如何为继承自 DimensionUnitTypeMeasurement 添加函数?

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)。不过,我计划实现的确切功能并不是这样工作的。

您需要使扩展本身通用,但是 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
    }
}