StackElementID

An opaque type that identifies an element of StackState.

StackReducer.swift:710
struct StackElementID

The StackState type creates instances of this identifier when new elements are added to the stack. This makes it possible to easily look up specific elements in the stack without resorting to positional indices, which can be error prone, especially when dealing with async effects.

The identifier is backed by a deterministic, generational ID. This allows you to predict how IDs will be created and allows you to write tests for how features behave in the stack.

@Test
func basics() {
  var path = StackState<Int>()
  path.append(42)
  XCTAssertEqual(path[id: 0], 42)
  path.append(1729)
  XCTAssertEqual(path[id: 1], 1729)

  path.removeAll()
  path.append(-1)
  XCTAssertEqual(path[id: 2], -1)
}

Notice that after removing all elements and appending a new element, the ID generated was 2 and did not go back to 0. This is because in tests the IDs are generational, which means they keep counting up, even if you remove elements from the stack.