Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
protocol Typed {
typealias Element
func elem() -> Element
}
struct Concrete<T>: Typed {
typealias Element = T
let e: T
func elem() -> T {
return e
}
}
class _AnyTypedBase<T>: Typed {
func elem() -> T {
fatalError()
}
}
class _AnyTyped<C: Typed>: _AnyTypedBase<C.Element> {
let base: C
init(base: C) {
self.base = base
}
override func elem() -> C.Element {
return base.elem()
}
}
final class AnyTyped<T>: Typed {
private let base: _AnyTypedBase<T>
init<C: Typed where C.Element == T>(base: C) {
self.base = _AnyTyped(base: base)
}
func elem() -> T {
return base.elem()
}
}
let concrete = Concrete(e: "Hi!")
let anyTyped = AnyTyped(base: concrete)
anyTyped.elem()
class _SAnySequenceBase<T>: SequenceType {
func generate() -> AnyGenerator<T> {
fatalError()
}
}
class _SAnySequence<S: SequenceType>: _SAnySequenceBase<S.Generator.Element> {
let base: S
init(base: S) {
self.base = base
}
override func generate() -> AnyGenerator<S.Generator.Element> {
return anyGenerator(base.generate())
}
}
final class SAnySequence<T>: SequenceType {
private let base: _SAnySequenceBase<T>
func generate() -> AnyGenerator<T> {
return base.generate()
}
init<S: SequenceType where S.Generator.Element == T>(base: S) {
self.base = _SAnySequence(base: base)
}
}
let array = ["a", "b", "c"]
let anySequence = SAnySequence(base: array)
anySequence.reduce("", combine: +)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment