File: optional.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (177 lines) | stat: -rw-r--r-- 4,607 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// RUN: %target-run-simple-swift | %FileCheck %s
// REQUIRES: executable_test

class A {
  func printA() { print("A", terminator: "") }
}
class B : A {
  override func printA() { print("B", terminator: "") }
}

func printA(_ v: A) { v.printA() }
func printOpt<T>(_ subprint: @escaping (T) -> ()) -> (T?) -> () {
  return { x in
    switch (x) {
    case .some(let y): print(".some(", terminator: ""); subprint(y); print(")", terminator: "")
    case .none: print(".none", terminator: "")
    }
  }
}

func test(_ v: A????, _ cast: (A????) -> B?) {
  printOpt(printOpt(printOpt(printOpt(printA))))(v)
  print(" as? B: ", terminator: "")
  printOpt(printA)(cast(v))
  print("\n", terminator: "")
}
test(.some(.some(.some(.some(A())))), { $0 as? B })
test(.some(.some(.some(.some(B())))), { $0 as? B })
test(.some(.some(.some(.none))), { $0 as? B })
test(.some(.some(.none)), { $0 as? B })
test(.some(.none), { $0 as? B })
test(.none, { $0 as? B })
// CHECK: .some(.some(.some(.some(A)))) as? B: .none
// CHECK: .some(.some(.some(.some(B)))) as? B: .some(B)
// CHECK: .some(.some(.some(.none))) as? B: .none
// CHECK: .some(.some(.none)) as? B: .none
// CHECK: .some(.none) as? B: .none
// CHECK: .none as? B: .none

func test(_ v: A????, _ cast: (A????) -> B??) {
  printOpt(printOpt(printOpt(printOpt(printA))))(v)
  print(" as? B?: ", terminator: "")
  printOpt(printOpt(printA))(cast(v))
  print("\n", terminator: "")
}
test(.some(.some(.some(.some(A())))), { $0 as? B? })
test(.some(.some(.some(.some(B())))), { $0 as? B? })
test(.some(.some(.some(.none))), { $0 as? B? })
test(.some(.some(.none)), { $0 as? B? })
test(.some(.none), { $0 as? B? })
test(.none, { $0 as? B? })
// CHECK: .some(.some(.some(.some(A)))) as? B?: .none
// CHECK: .some(.some(.some(.some(B)))) as? B?: .some(.some(B))
// CHECK: .some(.some(.some(.none))) as? B?: .some(.none)
// CHECK: .some(.some(.none)) as? B?: .none
// CHECK: .some(.none) as? B?: .none
// CHECK: .none as? B?: .none

func test(_ v: A????, _ cast: (A????) -> B???) {
  printOpt(printOpt(printOpt(printOpt(printA))))(v)
  print(" as? B??: ", terminator: "")
  printOpt(printOpt(printOpt(printA)))(cast(v))
  print("\n", terminator: "")
}
test(.some(.some(.some(.some(A())))), { $0 as? B?? })
test(.some(.some(.some(.some(B())))), { $0 as? B?? })
test(.some(.some(.some(.none))), { $0 as? B?? })
test(.some(.some(.none)), { $0 as? B?? })
test(.some(.none), { $0 as? B?? })
test(.none, { $0 as? B?? })
// CHECK: .some(.some(.some(.some(A)))) as? B??: .none
// CHECK: .some(.some(.some(.some(B)))) as? B??: .some(.some(.some(B)))
// CHECK: .some(.some(.some(.none))) as? B??: .some(.some(.none))
// CHECK: .some(.some(.none)) as? B??: .some(.none)
// CHECK: .some(.none) as? B??: .none
// CHECK: .none as? B??: .none

class Foo : Equatable {
}
func ==(a : Foo, b : Foo) -> Bool { return a === b }


var x_foo: Foo! = nil
if x_foo == nil { print("x_foo is nil") }
// CHECK: x_foo is nil
if x_foo != nil { print("x_foo is not nil") } else { print("x_foo is nil") }
// CHECK: x_foo is nil
if nil == x_foo { print("x_foo is nil") }
// CHECK: x_foo is nil
if nil != x_foo { print("x_foo is not nil") } else { print("x_foo is nil") }
// CHECK: x_foo is nil

var y_foo: Foo? = nil
if y_foo == nil { print("y_foo is nil") }
// CHECK: y_foo is nil
if y_foo != nil { print("y_foo is not nil") } else { print("y_foo is nil") }
// CHECK: y_foo is nil
if nil == y_foo { print("y_foo is nil") }
// CHECK: y_foo is nil
if nil != y_foo { print("y_foo is not nil") } else { print("y_foo is nil") }
// CHECK: y_foo is nil

var x : Int? = nil
var y : Int?? = x
var z : Int?? = nil

switch y {
  case nil:  print("y is nil")
  case .some(nil): print("y is .some(nil)")
  case .some(let v): print("y is .some(\(v))")
}
// CHECK: y is .some(nil)

switch z {
  case nil:  print("z is nil")
  case .some(nil): print("z is .some(nil)")
  case .some(let v): print("z is .some(\(v))")
}
// CHECK: z is nil

// Validate nil equality comparisons with non-equatable optional types
class C {}
var c: C? = nil

print(c == nil)
// CHECK: true

print(nil == c)
// CHECK: true

print(c != nil)
// CHECK: false

print(nil != c)
// CHECK: false

var c2: C? = C()

print(c2 == nil)
// CHECK: false

print(nil == c2)
// CHECK: false

print(c2 != nil)
// CHECK: true

print(nil != c2)
// CHECK: true

var c3: C! = nil

print(c3 == nil)
// CHECK: true

print(nil == c3)
// CHECK: true

print(c3 != nil)
// CHECK: false

print(nil != c3)
// CHECK: false

var c4: C! = C()

print(c4 == nil)
// CHECK: false

print(nil == c4)
// CHECK: false

print(c4 != nil)
// CHECK: true

print(nil != c4)
// CHECK: true