Skip to content

Instantly share code, notes, and snippets.

@marksands
Last active February 24, 2016 02:52
Show Gist options
  • Save marksands/81d536c3d7bc65585982 to your computer and use it in GitHub Desktop.
Save marksands/81d536c3d7bc65585982 to your computer and use it in GitHub Desktop.
Type Introspection Experimenting
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