File: parameterized_existential.swift

package info (click to toggle)
swiftlang 6.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,532 kB
  • sloc: cpp: 9,901,743; ansic: 2,201,431; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (114 lines) | stat: -rw-r--r-- 3,343 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// RUN: %target-typecheck-verify-swift -disable-availability-checking

protocol Sequence<Element> { // expected-note {{'Sequence' declared here}}
  associatedtype Element
}

// 'any' is required here

func takesSequenceOfInt1(_: Sequence<Int>) {}
// expected-warning@-1 {{use of protocol 'Sequence' as a type must be written 'any Sequence'}}

func returnsSequenceOfInt1() -> Sequence<Int> {}
// expected-warning@-1 {{use of protocol 'Sequence' as a type must be written 'any Sequence'}}

struct ConcreteSequence<Element> : Sequence {}

extension Sequence {
  func map<Other>(_ transform: (Self.Element) -> Other) -> ConcreteSequence<Other> {
    return ConcreteSequence<Other>()
  }
}

protocol DoubleWide<X, Y> {
  associatedtype X
  associatedtype Y

  var x: X { get }
  var y: Y { get }
}

extension Int: DoubleWide {
  typealias X = Int
  typealias Y = Int

  var x: X { 0 }
  var y: X { 0 }
}

struct Collapse<T: DoubleWide>: DoubleWide {
  typealias X = T
  typealias Y = T

  var x: X
  var y: X { self.x }
}

func test() -> any DoubleWide<some DoubleWide<Int, Int>, some DoubleWide<Int, Int>> { return Collapse<Int>(x: 42) }
// expected-error@-1 {{'some' types cannot be used in constraints on existential types}}

func diagonalizeAny(_ x: any Sequence<Int>) -> any Sequence<(Int, Int)> {
  return x.map { ($0, $0) }
}

func erase<T>(_ x: ConcreteSequence<T>) -> any Sequence<T> {
  return x as any Sequence<T>
}

protocol Sponge<A, B> {
  associatedtype A
  associatedtype B
}

func saturation(_ dry: any Sponge, _ wet: any Sponge<Int, Int>) {
  _ = dry as any Sponge<Int, Int>
  // expected-error@-1 {{'any Sponge' is not convertible to 'any Sponge<Int, Int>'}}
  // expected-note@-2 {{did you mean to use 'as!' to force downcast?}}
  _ = dry as any Sponge

  _ = wet as any Sponge<Int, Int> // Ok
  _ = wet as any Sponge // Ok
  _ = wet as any Sponge<String, String> // expected-error {{'any Sponge<Int, Int>' is not convertible to 'any Sponge<String, String>'}}
  // expected-note@-1 {{did you mean to use 'as!' to force downcast?}}
}

func typeExpr() {
  _ = Sequence<Int>.self
  // expected-warning@-1 {{use of protocol 'Sequence' as a type must be written 'any Sequence'}}

  _ = any Sequence<Int>.self
  // expected-error@-1 {{'self' is not a member type of protocol 'parameterized_existential.Sequence<Swift.Int>'}}

  _ = (any Sequence<Int>).self
}

/// Not supported as a protocol composition term for now

protocol SomeProto {}

func protocolCompositionNotSupported1(_: SomeProto & Sequence<Int>) {}
// expected-error@-1 {{non-protocol, non-class type 'Sequence<Int>' cannot be used within a protocol-constrained type}}

func protocolCompositionNotSupported2(_: any SomeProto & Sequence<Int>) {}
// expected-error@-1 {{non-protocol, non-class type 'Sequence<Int>' cannot be used within a protocol-constrained type}}

func increment(_ n : any Collection<Float>) {
  for value in n {
      _ = value + 1
  }
}

func genericIncrement<T: Numeric>(_ n : any Collection<T>) {
  for value in n {
    _ = value + 1
  }
}

protocol TextContainerViewProtocol {
    // expected-error@+1 {{optional 'any' type must be written '(any TextContainerViewDelegate<Self>)?'}}
    var delegate: any TextContainerViewDelegate<Self>? { get set}
}

protocol TextContainerViewDelegate<View> {
    associatedtype View: TextContainerViewProtocol
}