Last active
February 24, 2016 02:52
-
-
Save marksands/81d536c3d7bc65585982 to your computer and use it in GitHub Desktop.
Type Introspection Experimenting
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
import UIKit | |
import Foundation | |
struct IntrospectionType<T> { | |
let classType: Bool | |
let structType: Bool | |
let introspectionType: T | |
init(_ introspectionType: T) { | |
self.introspectionType = introspectionType | |
classType = false | |
structType = true | |
} | |
} | |
extension IntrospectionType where T: AnyObject { | |
init(_ introspectionType: T) { | |
self.introspectionType = introspectionType | |
classType = true | |
structType = false | |
} | |
} | |
public struct Sugar<HostType> { | |
public let host: HostType | |
public init(host: HostType) { | |
precondition(IntrospectionType<HostType>(host).structType) | |
self.host = host | |
} | |
} | |
public extension Sugar where HostType: AnyObject { | |
public init(host: HostType) { | |
precondition(IntrospectionType<HostType>(host).classType) | |
self.host = host | |
} | |
} | |
public protocol RXSObject: AnyObject {} | |
public extension RXSObject { | |
typealias RxsSelfType = Self | |
public var rxs: Sugar<RxsSelfType> { return Sugar(host: self) } | |
} | |
class SomeClassType: RXSObject { | |
} | |
struct SomeValueType { | |
var rxs: Sugar<SomeValueType> { return Sugar(host: self) } | |
} | |
let c = SomeClassType() | |
c.rxs.host | |
let s = SomeValueType() | |
s.rxs.host | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment