Skip to content

Commit 86fb3fa

Browse files
chriseidhofairspeedswift
authored andcommitted
[wip] Bool toggle implementation (#14586)
* Bool toggle implementation * Made it public
1 parent 571522e commit 86fb3fa

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

stdlib/public/core/Bool.swift

+16
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,19 @@ extension Bool {
298298
return lhs ? true : try rhs()
299299
}
300300
}
301+
302+
extension Bool {
303+
@_inlineable
304+
/// Toggles the value of the Boolean.
305+
///
306+
/// Calling this method sets the variable to `true` if it was `false`,
307+
/// and sets it to `false` if it was `true`. For example:
308+
///
309+
/// var bools = [true, false]
310+
///
311+
/// bools[0].toggle()
312+
/// // bools now contains [false, false]
313+
public mutating func toggle() {
314+
self = !self
315+
}
316+
}

validation-test/stdlib/Bool.swift

+9-1
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,12 @@ BoolTestSuite.test("FalsePositive") {
4141
expectNotEqual(30, result)
4242
}
4343

44-
runAllTests()
44+
BoolTestSuite.test("Toggle") {
45+
var result = [false, true, true]
46+
result[0].toggle()
47+
result[1].toggle()
48+
expectEqual([true, false, true], result)
49+
}
50+
51+
52+
runAllTests()

0 commit comments

Comments
 (0)