12
12
// Boolean
13
13
//===----------------------------------------------------------------------===//
14
14
15
- /// Returns the result of inverting `a`'s logic value.
15
+ /// Performs a logical NOT operation on a Boolean value.
16
+ ///
17
+ /// The `!` (logical NOT) operator inverts a Boolean value. If the value is
18
+ /// `true`, the result of the operation is `false`; if the value is `false`,
19
+ /// the result is `true`. For example:
20
+ ///
21
+ /// var printedMessage = false
22
+ ///
23
+ /// if !printedMessage {
24
+ /// print("You look nice today!")
25
+ /// printedMessage = true
26
+ /// }
27
+ /// // Prints "You look nice today!"
28
+ ///
29
+ /// - Parameter a: The Boolean value to negate.
16
30
@warn_unused_result
17
31
public prefix func ! < T : Boolean > ( a: T ) -> Bool {
18
32
return !a. boolValue
19
33
}
20
34
21
- /// If `lhs` is `false`, return it. Otherwise, evaluate `rhs` and
22
- /// return its `boolValue`.
35
+ /// Performs a logical AND operation on two Boolean values.
36
+ ///
37
+ /// The `&&` (logical AND) operator combines two Boolean values and returns
38
+ /// `true` if both of the values are `true`. If either of the values is
39
+ /// `false`, the operator returns `false`.
40
+ ///
41
+ /// This operator uses short-circuit evaluation: The left-hand side (`lhs`) is
42
+ /// evaluated first, and the right-hand side (`rhs`) is evaluated only if
43
+ /// `lhs` evaluates to `true`. For example:
44
+ ///
45
+ /// let measurements = [7.44, 6.51, 4.74, 5.88, 6.27, 6.12, 7.76]
46
+ /// let sum = measurements.reduce(0, combine: +)
47
+ ///
48
+ /// if measurements.count > 0 && sum / Double(measurements.count) < 6.5 {
49
+ /// print("Average measurement is less than 6.5")
50
+ /// }
51
+ /// // Prints "Average measurement is less than 6.5"
52
+ ///
53
+ /// In this example, `lhs` tests whether `measurements.count` is greater than
54
+ /// zero. Evaluation of the `&&` operator is one of the following:
55
+ ///
56
+ /// - When `measurements.count` is equal to zero, `lhs` evaluates to `false`
57
+ /// and `rhs` is not evaluated, preventing a divide-by-zero error in the
58
+ /// expression `sum / Double(measurements.count)`. The result of the
59
+ /// operation is `false`.
60
+ /// - When `measurements.count` is greater than zero, `lhs` evaluates to `true`
61
+ /// and `rhs` is evaluated. The result of evaluating `rhs` is the result of
62
+ /// the `&&` operation.
63
+ ///
64
+ /// - Parameters:
65
+ /// - lhs: The left-hand side of the operation.
66
+ /// - rhs: The right-hand side of the operation.
23
67
@inline ( __always)
24
68
@warn_unused_result
25
69
public func && < T : Boolean , U : Boolean > (
@@ -28,8 +72,39 @@ public func && <T : Boolean, U : Boolean>(
28
72
return lhs. boolValue ? try rhs ( ) . boolValue : false
29
73
}
30
74
31
- /// If `lhs` is `true`, return it. Otherwise, evaluate `rhs` and
32
- /// return its `boolValue`.
75
+ /// Performs a logical OR operation on two Boolean values.
76
+ ///
77
+ /// The `||` (logical OR) operator combines two Boolean values and returns
78
+ /// `true` if at least one of the values is `true`. If both values are
79
+ /// `false`, the operator returns `false`.
80
+ ///
81
+ /// This operator uses short-circuit evaluation: The left-hand side (`lhs`) is
82
+ /// evaluated first, and the right-hand side (`rhs`) is evaluated only if
83
+ /// `lhs` evaluates to `false`. For example:
84
+ ///
85
+ /// let majorErrors: Set = ["No first name", "No last name", ...]
86
+ /// let error = ""
87
+ ///
88
+ /// if error.isEmpty || !majorErrors.contains(error) {
89
+ /// print("No major errors detected")
90
+ /// } else {
91
+ /// print("Major error: \(error)")
92
+ /// }
93
+ /// // Prints "No major errors detected")
94
+ ///
95
+ /// In this example, `lhs` tests whether `error` is an empty string. Evaluation
96
+ /// of the `||` operator is one of the following:
97
+ ///
98
+ /// - When `error` is an empty string, `lhs` evaluates to `true` and `rhs` is
99
+ /// not evaluated, skipping the call to `majorErrors.contains(_:)`. The
100
+ /// result of the operation is `true`.
101
+ /// - When `error` is not an empty string, `lhs` evaluates to `false` and `rhs`
102
+ /// is evaluated. The result of evaluating `rhs` is the result of the `||`
103
+ /// operation.
104
+ ///
105
+ /// - Parameters:
106
+ /// - lhs: The left-hand side of the operation.
107
+ /// - rhs: The right-hand side of the operation.
33
108
@inline ( __always)
34
109
@warn_unused_result
35
110
public func || < T : Boolean , U : Boolean > (
@@ -38,6 +113,38 @@ public func || <T : Boolean, U : Boolean>(
38
113
return lhs. boolValue ? true : try rhs ( ) . boolValue
39
114
}
40
115
116
+ /// Performs a logical AND operation on two Boolean values.
117
+ ///
118
+ /// The `&&` (logical AND) operator combines two Boolean values and returns
119
+ /// `true` if both of the values are `true`. If either of the values is
120
+ /// `false`, the operator returns `false`.
121
+ ///
122
+ /// This operator uses short-circuit evaluation: The left-hand side (`lhs`) is
123
+ /// evaluated first, and the right-hand side (`rhs`) is evaluated only if
124
+ /// `lhs` evaluates to `true`. For example:
125
+ ///
126
+ /// let measurements = [7.44, 6.51, 4.74, 5.88, 6.27, 6.12, 7.76]
127
+ /// let sum = measurements.reduce(0, combine: +)
128
+ ///
129
+ /// if measurements.count > 0 && sum / Double(measurements.count) < 6.5 {
130
+ /// print("Average measurement is less than 6.5")
131
+ /// }
132
+ /// // Prints "Average measurement is less than 6.5"
133
+ ///
134
+ /// In this example, `lhs` tests whether `measurements.count` is greater than
135
+ /// zero. Evaluation of the `&&` operator is one of the following:
136
+ ///
137
+ /// - When `measurements.count` is equal to zero, `lhs` evaluates to `false`
138
+ /// and `rhs` is not evaluated, preventing a divide-by-zero error in the
139
+ /// expression `sum / Double(measurements.count)`. The result of the
140
+ /// operation is `false`.
141
+ /// - When `measurements.count` is greater than zero, `lhs` evaluates to `true`
142
+ /// and `rhs` is evaluated. The result of evaluating `rhs` is the result of
143
+ /// the `&&` operation.
144
+ ///
145
+ /// - Parameters:
146
+ /// - lhs: The left-hand side of the operation.
147
+ /// - rhs: The right-hand side of the operation.
41
148
// FIXME: We can't make the above @_transparent due to
42
149
// rdar://problem/19418937, so here are some @_transparent overloads
43
150
// for Bool. We've done the same for ObjCBool.
@@ -49,6 +156,39 @@ public func && <T : Boolean>(
49
156
return lhs. boolValue ? try rhs ( ) . boolValue : false
50
157
}
51
158
159
+ /// Performs a logical OR operation on two Boolean values.
160
+ ///
161
+ /// The `||` (logical OR) operator combines two Boolean values and returns
162
+ /// `true` if at least one of the values is `true`. If both values are
163
+ /// `false`, the operator returns `false`.
164
+ ///
165
+ /// This operator uses short-circuit evaluation: The left-hand side (`lhs`) is
166
+ /// evaluated first, and the right-hand side (`rhs`) is evaluated only if
167
+ /// `lhs` evaluates to `false`. For example:
168
+ ///
169
+ /// let majorErrors: Set = ["No first name", "No last name", ...]
170
+ /// let error = ""
171
+ ///
172
+ /// if error.isEmpty || !majorErrors.contains(error) {
173
+ /// print("No major errors detected")
174
+ /// } else {
175
+ /// print("Major error: \(error)")
176
+ /// }
177
+ /// // Prints "No major errors detected")
178
+ ///
179
+ /// In this example, `lhs` tests whether `error` is an empty string. Evaluation
180
+ /// of the `||` operator is one of the following:
181
+ ///
182
+ /// - When `error` is an empty string, `lhs` evaluates to `true` and `rhs` is
183
+ /// not evaluated, skipping the call to `majorErrors.contains(_:)`. The
184
+ /// result of the operation is `true`.
185
+ /// - When `error` is not an empty string, `lhs` evaluates to `false` and `rhs`
186
+ /// is evaluated. The result of evaluating `rhs` is the result of the `||`
187
+ /// operation.
188
+ ///
189
+ /// - Parameters:
190
+ /// - lhs: The left-hand side of the operation.
191
+ /// - rhs: The right-hand side of the operation.
52
192
@_transparent
53
193
@warn_unused_result
54
194
public func || < T : Boolean > (
0 commit comments