mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Protocols with associated types can't currently be used as existential types. Combined with the inability to create type constraints on generic functions nested in generic types based on the outer type and the inability to create closures of generic type and the inability to create protocol constraints that require generic functions and the inability to create protocols with init() requirements... and this is what we get. Swift SVN r10034
49 lines
1.1 KiB
Swift
49 lines
1.1 KiB
Swift
//////////////////////////////////////////
|
|
// FIXME: Workaround for inability to create existentials of protocols
|
|
// with associated types <rdar://problem/11689181> and for the
|
|
// inability to constrain nested generics based on the containing type
|
|
// <rdar://problem/11700999>
|
|
//
|
|
|
|
// This file contains "existentials" for the protocols defined in
|
|
// Policy.swift. Similar components should usually be defined next to
|
|
// their respective protocols.
|
|
struct GeneratorOf<T> : Generator {
|
|
def next() -> T? {
|
|
return _next()
|
|
}
|
|
var _next : ()->T?
|
|
}
|
|
|
|
def existential<G: Generator>(base: G)
|
|
-> GeneratorOf<G.Element>
|
|
{
|
|
return GeneratorOf( { base.next() } )
|
|
}
|
|
|
|
struct EnumerableOf<T> : Enumerable {
|
|
def enumerate() -> GeneratorOf<T> {
|
|
return _enumerate()
|
|
}
|
|
var _enumerate : ()->GeneratorOf<T>
|
|
}
|
|
|
|
def existential<E: Enumerable>(base: E)
|
|
-> EnumerableOf<E.GeneratorType.Element>
|
|
{
|
|
return EnumerableOf<E.GeneratorType.Element>(
|
|
{ existential(base.enumerate()) }
|
|
)
|
|
}
|
|
|
|
struct SinkOf<T> : Sink {
|
|
def put(x: T) {
|
|
_put(x)
|
|
}
|
|
var _put : (T)->()
|
|
}
|
|
|
|
def existential<S: Sink>(base: S) -> SinkOf<S.Element> {
|
|
return SinkOf( { base.put($0) } )
|
|
}
|