Skip to content

Instantly share code, notes, and snippets.

@landonf
Last active March 28, 2018 08:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save landonf/e01b51ce34766f4cfa98 to your computer and use it in GitHub Desktop.
Save landonf/e01b51ce34766f4cfa98 to your computer and use it in GitHub Desktop.
An attempt to use the type inferencer to explicitly bind a protocol's typealias.
protocol TypeBinder {
typealias BoundT
}
class TypeEvidence<T> : TypeBinder {
typealias BoundT = T
func array () -> Array<BoundT> { return [] }
}
class ObserverTyper<T> {
class func bind <O: Observer where O.EventType == T> () -> TypeEvidence<O> { return TypeEvidence<O>() }
}
class Observable<T> : Stream<T> {
// Breaks the type inferencer?
// error: cannot convert the expression's type 'Array<$T2>' to type 'Observer'
var observers = ObserverTyper<T>.bind().array()
// but this works just fine ... so shouldn't the above work, too?
func sendExample<OT, O: Observer where O.EventType == OT> (value: OT) {
let observers = Array<O>()
for observer in observers {
observer.send(Event<OT>.Next(value))
}
}
}
class Stream<T> {
}
protocol Observer {
typealias EventType
func send(Event<EventType>)
}
enum Event<T> {
case Next(T)
//case Error(NSError)
case Completed
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment