Skip to content

Instantly share code, notes, and snippets.

@Dev1an
Created August 7, 2017 22:11
Show Gist options
  • Save Dev1an/2d5c87a2a5b68ab2c8af2889dd6e2148 to your computer and use it in GitHub Desktop.
Save Dev1an/2d5c87a2a5b68ab2c8af2889dd6e2148 to your computer and use it in GitHub Desktop.
//: Playground - noun: a place where people can play
import Foundation
protocol MyProtocol {
var count: UInt {get}
mutating func increment()
}
struct MyStruct<T: MyProtocol>: MyGenericStructProtocol {
var t: T
}
protocol MyGenericStructProtocol {
associatedtype GenericType: MyProtocol
var t: GenericType { get set }
}
extension Array where Element: MyGenericStructProtocol {
func doWork() -> [Element.GenericType] {
return map {
var incremented = $0.t
incremented.increment()
return incremented
}
}
}
struct SimpleCounter: MyProtocol {
var count: UInt
mutating func increment() { count += 1 }
}
[MyStruct(t: SimpleCounter(count: 5)), MyStruct(t: SimpleCounter(count: 0))].doWork()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment