liveBook · Manning

Equality-based conflation

As explained in Kotlin in Action, 2E

Equality-based conflation suppresses a new stream value when it is equal to the value currently represented or most recently emitted.

Equality-based conflation suppresses a new state-flow emission when the assigned value equals the value already represented. It keeps state updates focused on actual changes rather than repeated assignments. section 16.3.2 section 16.4

Repeated direction assignments

A DirectionSelector starts at LEFT and updates its state with _direction.update { d }. A collector logs LEFT, RIGHT, and LEFT when those values change; a later turn(Direction.LEFT) produces no additional log because LEFT is already represented. section 16.3.2

Figure 16.6 State flows perform equality-based conflation. When the value is assigned the same value as is already set, no new elements are emitted.
CH16_F06_Isakova

Use the figure to see the rule directly: assigning the current value again emits no new element. section 16.3.2

The technique belongs to StateFlow, which represents one current value. SharedFlow instead broadcasts emissions to subscribers, so reframing a message stream as state—such as a growing message-history list—can let later collectors observe prior updates. section 16.3.3

How Kotlin in Action, 2E covers it

How deep it goes
Defines repeated-value suppression and demonstrates it with a direction collector, but does not detail equality implementation internals.
Examples
Uses a DirectionSelector that ignores a repeated LEFT assignment and a message-history list stored in StateFlow.
What it assumes
Assumes familiarity with Kotlin flows, StateFlow, SharedFlow, and collectors while teaching the conflation rule through examples.
What you can do after
Operate StateFlow updates with an expectation that unchanged values do not reach collectors.
How it connects
Connects StateFlow with SharedFlow, hot flows, collectors, and state updates.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kotlin in Action, 2E. The section links point to the parts of the book it draws on.