【发布时间】:2016-09-24 20:37:23
【问题描述】:
我想在 Swift 中使用一阶公民类型来决定调用哪个函数。
func sf(v: [Float]) -> Float{
}
func df(v: [Double]) -> Double {
}
func f<T: RealType>(seq ls: [T]) -> T {
if T.self == Float.self {
return sf(ls) // 1. where sf: Float -> Void
} else if T.self == Double.self {
return df(ls) // 2. where df : Double -> Void
}
}
类型推断系统没有注意到在一个分支下 T == Float 和 Double 在另一个分支下?
这里是否缺少功能、复杂功能或错误?
编辑:
typealias RealType = protocol<FloatingPointType, Hashable, Equatable, Comparable, FloatLiteralConvertible, SignedNumberType>
对于我的原型,但将成为协议
【问题讨论】:
-
您的代码中如何定义
RealType? -
RealType 目前是一个类型别名。我编辑了我的问题以添加它。