Add swap into Set Collection Type

Hey SE!

I did not see any proposal about swich an element of Set Collection, so here I am.
First, what I mean with "swich an element". Sometimes I use a Set as a Toggle Manager to persists what elements I turned "on". When this happen I always need to add the follow swich method to the Set.

extension Set {
    mutating func `switch`(_ element: Element) {
        if self.contains(element) {
        } else {

As it happens all the time, I am thinking if this is not a thing that already could be as part of Set struct.
What do you all think about it?

In set algebra, this is called the symmetric difference. Swift offers the methods symmetricDifference (nonmutating) and formSymmetricDifference (mutating). So, you would write:


Also, if you really like your extension, you can spell it right if you want by using backticks:

extension SetAlgebra {
  mutating func `switch`(_ element: ArrayLiteralElement) {
var bats: Set = ["🏏", "πŸ¦‡"]
bats.switch("🏏")  // {"πŸ¦‡"}
bats.switch("🏏")  // {"πŸ¦‡", "🏏"}
1 Like

I made something similar using a different approach: exposing the presence of an element as a boolean value through a subscript:

var ingredients: Set<String> = ["water", "potato", "carrot"]

// what this pitch is about:
ingredients["potato"].toggle() // insert or remove "potato"

// other usages:
ingredients["carrot"] = false // removes "carrot" from the set
ingredients["onion"] = true // inserts "onion"
ingredients["sugar"] = client.likesSweetenedThings
The SetAlgebra Extension
extension SetAlgebra {

	/// A subscript that returns a boolean indicating whether the member is
	/// present in the set. Assiging a boolean value to the subscript will
	/// insert or remove the member from the script.
	/// - Parameter member: An element to look for in the set.
	public subscript (member: Element) -> Bool {
		get {
			return contains(member)
		set {
			if newValue {
			} else {


Whether this belongs in the standard library, I have some doubts. I find it quite useful for what I'm doing, but those aren't exactly the semantics you'd expect of a set.


That getter is useless, right? To me, that just looks like another use case for set-only subscripts/properties.

The getter is needed when you want to call .toggle() on the boolean value (which this pitch is all about).


I see. I didn't realize you were actually trying to support


So no, you're not talking about set-only properties. But you are talking about named subscripts, another one of my favorite missing features*! :smiley_cat:

And I'm in agreement that contains should be one of those, instead of a method.

var set: Set = [1, 2, 3]


set.contains[1] = false
XCTAssertEqual(set, [2, 3])

var contains = set.contains

XCTAssertEqual(set, [2])

contains.set = { _, _, _ in }
XCTAssertEqual(set, [2])

var four: Set = [4]
withUnsafeMutablePointer(to: &four) {
  contains.pointer = $0

The way we can express this is not good enough, because it won't work on constants. Currently, you need to take an Objective-C-like approach, with functions/properties for the use case of get-only named subscripts

public extension SetAlgebra {
  var contains: Subscript<Self, Element, Bool> {
    mutating get {
        get: Self.contains,
        set: { set, element, newValue in
          if newValue {
          } else {

The way we can implement it ourselves works, but it'll break at some point if you abuse it past the one-liner typical use case. That is not good enough. They need to be in the language.

/// An emulation of the missing Swift feature of named subscripts.
/// - Note: Argument labels are not supported.
public struct Subscript<Root, Index, Value> {
  public typealias Pointer = UnsafeMutablePointer<Root>
  public typealias Get = (Root) -> (Index) -> Value
  public typealias Set = (inout Root, Index, Value) -> Void

  public var pointer: Pointer
  public var get: Get
  public var set: Set

public extension Subscript {
    _ pointer: Pointer,
    get: @escaping Get,
    set: @escaping Set
  ) {
    self.pointer = pointer
    self.get = get
    self.set = set

  subscript(index: Index) -> Value {
    get { get(pointer.pointee)(index) }
    nonmutating set { set(&pointer.pointee, index, newValue) }

…* Now that we have callAsFunction, there's no reason for both () and [] syntax anymore. I say "named subscript", because that's what this kind of thing would be in older languages, but we probably should get rid of [] and allow assignment to the result of ()s

using pointers that aren't valid anymore :scream:

1 Like


This is what we resort to when inout arguments can't be used in subscripts, and subscripts can't be chained to initializers.

That’s... quite an extreme measure to dive into undefined behavior territory, especially when the next viable option is to use argument labelβ€”set[contains: value].toggle()


This is actually very useful for creating bindings to sets in SwiftUI:

struct SelectView: View {
    var items: [String] = ["A", "B", "C"]
    @State var selectedItems: Set<String> = []

    var body: some View {
        VStack {
            ForEach(items, id: \.self) { item in
                Toggle(isOn: self.$selectedItems[item]) {

In fact, I think these are exactly the semantics you expect from a set. Something is either in the set or not, true or false. It might still not belong in the standard library though – the implementation is pretty trivial, and it’s hard to get it wrong.


I would love to see this, as it's a natural extension to Bool.toggle(), and I think this sort of API makes the most sense:

extension SetAlgebra {
    /// insert the element if it's not in the set, otherwise remove it
    /// returns the contained state of the element after toggling
    public mutating func toggle(_ element: Element) -> Bool {
        if contains(element) {
            return false
        } else {
            return true


This would make it easy to do things like:

setOfStrings.toggle("string 1")

and so on.

Terms of Service

Privacy Policy

Cookie Policy