Created
August 7, 2017 22:11
-
-
Save Dev1an/2d5c87a2a5b68ab2c8af2889dd6e2148 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: 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