Skip to content

Instantly share code, notes, and snippets.

@huguesbr
Created April 6, 2017 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huguesbr/a110e77d06b34962710d1b9877128dd9 to your computer and use it in GitHub Desktop.
Save huguesbr/a110e77d06b34962710d1b9877128dd9 to your computer and use it in GitHub Desktop.
Extension wrapper
// 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