CustomDump Documentation

Function diff(_:​_:​format:​)

public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String?  

Detects differences between two given values by comparing their mirrors and optionally returns a formatted string describing it.

This can be a great tool to use for building debug tools for applications and libraries. For example, this library uses diff(_:_:format:) to implement XCTAssertNoDifference(_:_:_:file:line:), which asserts that two values are equal, and if they are not the failure message is a nicely formatted diff showing exactly what part of the values are not equal.

Further, the Composable Architecture uses diff(_:_:format:) in a couple different ways:

  • It is used to implement a tool that prints changes to application state over time as diffs between the previous state and the current state whenever an action is sent to the store.

  • It is also used in a testing tool so that when one fails to assert for how state may have changed after sending an action, it can display a concise message showing the exact difference in state.

Parameters

lhs T

An expression of type T.

rhs T

A second expression of type T.

format Diff​Format

A format to use for the diff. By default it uses ASCII characters typically associated with the "diff" format: "-" for removals, "+" for additions, and " " for unchanged lines.

Returns

A string describing any difference detected between values, or nil if no difference is detected.