Created
April 6, 2017 10:19
-
-
Save huguesbr/a110e77d06b34962710d1b9877128dd9 to your computer and use it in GitHub Desktop.
Extension wrapper
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
// exploration | |
// add a extension wrapper to any type (like `Reactive` from `RxSwift`) | |
//: Playground - noun: a place where people can play | |
import UIKit | |
struct Extension<Base> { | |
let base: Base | |
init(_ base: Base) { | |
self.base = base | |
} | |
} | |
protocol ExtensionCompatible { | |
associatedtype CompatibleType | |
static var `extension`: Extension<CompatibleType>.Type { get set } | |
var `extension`: Extension<CompatibleType> { get set } | |
} | |
extension ExtensionCompatible { | |
static var `extension`: Extension<Self>.Type { | |
get { | |
return Extension<Self>.self | |
} | |
set { | |
// mutate | |
} | |
} | |
var `extension`: Extension<Self> { | |
get { | |
return Extension(self) | |
} | |
set { | |
// mutate | |
} | |
} | |
} | |
extension Extension where Base == Data { | |
var blah: String { return "blah" } | |
} | |
extension Data: ExtensionCompatible {} | |
// usage | |
let data = Data(bytes: [1, 2, 3]) | |
print(data.extension.blah) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment