Extend Array Types Using Where Clause in Swift

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() {
...
}
}

Extension of Set where element is an Array of a specific type

Since you cannot make Array<NSOperation> conform to Hashable you'll have to make a small wrapper-struct.

E.g.

struct NSOperationList {

var operations = [NSOperation]()
}

and then build all functionality you need on top of NSOperationList.

So if you want to add support for Set:

extension NSOperationList: Hashable {

var hashValue: Int {
return operations.reduce(0) { $0 ^ $1.hashValue }
}
}

func == (a: NSOperationList, b: NSOperationList) -> Bool {
return a.operations == b.operations
}

Swift: Extend Array with generics method with namespace

Finally, I found a workaround.

I change the code in Array+CC.swift:

extension Array: CCCompatible {}

extension CCWrapper where Wrapped: Sequence {
public func find(_ predicate: (Wrapped.Iterator.Element) -> Bool) -> Wrapped.Iterator.Element? {
for e in wrapped where predicate(e) { return e }
return nil
}
}

How to extend a CollectionType with a Protocol and a where clause?

You can define extension methods which apply only to a restricted
type of the generic placeholders

extension CollectionType where Generator.Element == DataPoint {
var barChartData: BarChartData { return somethingUseful }
}

and then

[DataPoint(), DataPoint()].barChartData

compiles. But you cannot declare a "conditional conformance to
a protocol", such as

extension CollectionType: DataPoint where Generator.Element == DataPoint { ... }

Such a feature is discussed on the Swift Evolution mailing list,
starting at [swift-evolution] [Manifesto] Completing Generics:

*Conditional conformances

Conditional conformances express the notion that a generic type will
conform to a particular protocol only under certain circumstances. For
example, Array is Equatable only when its elements are Equatable:

extension Array : Equatable where Element : Equatable { }

but it is not available in Swift 2 and – as far as I can see –
not on the current lists of proposals for Swift 3 at https://github.com/apple/swift-evolution.

How can I extend typed Arrays in Swift?

For extending typed arrays with classes, the below works for me (Swift 2.2). For example, sorting a typed array:

class HighScoreEntry {
let score:Int
}

extension Array where Element == HighScoreEntry {
func sort() -> [HighScoreEntry] {
return sort { $0.score < $1.score }
}
}

Trying to do this with a struct or typealias will give an error:

Type 'Element' constrained to a non-protocol type 'HighScoreEntry'

Update:

To extend typed arrays with non-classes use the following approach:

typealias HighScoreEntry = (Int)

extension SequenceType where Generator.Element == HighScoreEntry {
func sort() -> [HighScoreEntry] {
return sort { $0 < $1 }
}
}

In Swift 3 some types have been renamed:

extension Sequence where Iterator.Element == HighScoreEntry 
{
// ...
}


Related Topics



Leave a reply



Submit