问题描述
对于一些附加功能,我想扩展 ClosedRange
,其中边界为 Measurement
。
extension ClosedRange where Bound == Measurement
...显然不能像 Measurement
那样工作,因为泛型类型需要 <>
参数。
extension ClosedRange where Bound == Measurement<UnitType> // Cannot find type 'UnitType' in scope
连接 where
似乎也不起作用。
我没有想法。
我发现唯一有效的是:
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)
range.unit
编辑:
感谢 Dávid Pásztor 的回答。我想扩展我的问题:
如何为具有继承自 Measurement
的 UnitType
的 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)
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
}
}