Extend array types using where clause in Swift
If you want to extend only array with specific type. You should extend _ArrayType protocol.
extension _ArrayType where Generator.Element == Int {
func doSomething() {
...
}
}
If you extend Array
you can only make sure your element is conformed some protocol else. i.e:
extension Array where Element: Equatable {
func doSomething() {
...
}
}
Updated: With Swift 3.1 https://github.com/apple/swift/blob/master/CHANGELOG.md
extension Array where Element == Int {
func doSomething() {
...
}
}
Swift 3 to the rescue!!
extension Collection where Iterator.Element == Int {
// `Collection` can be `Sequence`, etc
}
How about
extension CollectionType where Generator.Element == Double {
}
Or If you want a little bit more:
protocol ArithmeticType {
func +(lhs: Self, rhs: Self) -> Self
func -(lhs: Self, rhs: Self) -> Self
func *(lhs: Self, rhs: Self) -> Self
func /(lhs: Self, rhs: Self) -> Self
}
extension Double : ArithmeticType {}
extension Float : ArithmeticType {}
extension SequenceType where Generator.Element : protocol<FloatLiteralConvertible, ArithmeticType> {
var sum : Generator.Element {
return reduce(0.0, combine: +)
}
var product : Generator.Element {
return reduce(1.0, combine: *)
}
}
stride(from: 1.0, through: 10.0, by: 1.0).sum // 55
[1.5, 2.0, 3.5, 4.0, 5.5].product // 231
Works with Double
and Float
or any other type that you conform to the protocols ArithmeticType
and FloatLiteralConvertible
. If you need to access specific indices of your array, change SequenceType
to CollectionType
as you cannot do this with a sequence.
If you only want to extend a specific Array
you have to use a protocol for each type:
protocol DoubleValue {
var value: Double { get }
}
extension Double: DoubleValue {
var value: Double { return self }
}
extension Array where Element: DoubleValue {
// use the value property
}
// the same for Float
protocol FloatValue {
var value: Float { get }
}
extension Float: FloatValue {
var value: Float { return self }
}
extension Array where Element: FloatValue {
// use the value property
}
So I didn't read the question properly. FloatingPointType
is an existing protocol that is implemented by Double, Float and CGFloat, so
Yes. I did it only yesterday to add a function to SequenceType where the elements had to be Equatable
. This is a modification to restrict the elements to Float
You need to use a where clause. This is my function below.
public extension SequenceType where Self.Generator.Element: FloatingPointType
{
public func splitAt(separator: Generator.Element) -> [[Generator.Element]]
{
var ret: [[Generator.Element]] = []
var thisPart: [Generator.Element] = []
for element in self
{
if element == separator
{
ret.append(thisPart)
thisPart = []
}
else
{
thisPart.append(element)
}
}
ret.append(thisPart)
return ret
}
}
[Float(1), Float(2), Float(3), Float(4)].splitAt(Float(2))
// returns [[1],[3, 4]]
[Double(1), Double(2), Double(3), Double(4)].splitAt(Double(3))
// returns [[1, 2],[4]]
NB I couldn't make this work for an array but SequenceType is more general anyway.