|
| 1 | +// RUN: %empty-directory(%t) |
| 2 | +// RUN: %target-build-swift -swift-version 5 -Xfrontend -enable-experimental-named-opaque-types %s -o %t/a.out |
| 3 | +// RUN: %target-run %t/a.out | %FileCheck %s |
| 4 | + |
| 5 | +// REQUIRES: executable_test |
| 6 | + |
| 7 | +@available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) |
| 8 | +dynamic func lazyMapCollection<C: Collection, T>(_ collection: C, body: @escaping (C.Element) -> T) |
| 9 | + -> <R: Collection where R.Element == T> R { |
| 10 | + return collection.lazy.map { body($0) } |
| 11 | +} |
| 12 | + |
| 13 | +@available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) |
| 14 | +dynamic func lazyMapCollection2<C: Collection, T>(_ collection: C, body: @escaping (C.Element) -> T) |
| 15 | + -> <R: Collection where R.Element == T> R { |
| 16 | + collection.lazy.map { body($0) } |
| 17 | +} |
| 18 | + |
| 19 | +@available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) |
| 20 | +dynamic func unzipCollection<C: Collection, T, U: Hashable>(_ collection: C) |
| 21 | +-> <R1: Collection, R2: Collection where R1.Element == T, R2.Element == U> (R1, R2) where C.Element == (T, U) { |
| 22 | + return (Array(collection.map { $0.0 }), Set(collection.map { $0.1 })) |
| 23 | +} |
| 24 | + |
| 25 | +protocol P { |
| 26 | + associatedtype A |
| 27 | + func f() -> A |
| 28 | +} |
| 29 | + |
| 30 | +@available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) |
| 31 | +struct X<T, U: Hashable>: P { |
| 32 | + var data: [(T, U)] |
| 33 | + |
| 34 | + func f() -> < |
| 35 | + R1: Collection, R2: Collection where R1.Element == T, R2.Element == U |
| 36 | + > (R1, R2) { |
| 37 | + // FIXME: Use unzipCollection here |
| 38 | + return (Array(data.map { $0.0 }), Set(data.map { $0.1 })) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func getP_A<T: P>(_: T.Type) -> Any.Type { |
| 43 | + return T.A.self |
| 44 | +} |
| 45 | + |
| 46 | +if #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) { |
| 47 | + // CHECK: {{[1.0, 2.0, 3.0]|too old}} |
| 48 | + let array = [1, 2, 3] |
| 49 | + let x = lazyMapCollection(array) { Double($0) } |
| 50 | + print(x) |
| 51 | + |
| 52 | + // CHECK: {{[0.5, 1.0, 1.5]|too old}} |
| 53 | + let x2 = lazyMapCollection2(array) { Double($0) / 2.0 } |
| 54 | + print(x2) |
| 55 | + |
| 56 | + let array2 = [(1, "Hello"), (2, "World"), (3, "!")] |
| 57 | + let x3 = unzipCollection(array2) |
| 58 | + |
| 59 | + // CHECK: {{[1, 2, 3]|too old}} |
| 60 | + print(x3.0) |
| 61 | + |
| 62 | + // CHECK: {{["Hello", "World", "!"]|too old}} |
| 63 | + print(x3.1) |
| 64 | + |
| 65 | + // CHECK: {{(Array<Int>, Set<String>)|too old}} |
| 66 | + let paType = getP_A(X<Int, String>.self) |
| 67 | + print(paType) |
| 68 | + typealias ExpectedPAType = ([Int], Set<String>) |
| 69 | + assert(paType == ExpectedPAType.self) |
| 70 | +} else { |
| 71 | + print("too old") |
| 72 | + print("too old") |
| 73 | + print("too old") |
| 74 | + print("too old") |
| 75 | + print("too old") |
| 76 | +} |
0 commit comments