Kotlin 2.4.20 Introduces Long-Awaited Collection Utilities and Syntax Improvements
New allDistinctBy/allEqualBy functions and context-sensitive resolution reduce boilerplate in Kotlin code.
1New Collection Utilities
Kotlin 2.4.20 introduces allDistinctBy and allEqualBy functions for collections, providing more efficient ways to check uniqueness. Unlike previous approaches that required creating intermediate collections, these new functions perform checks in a single pass. They work with Iterable, Sequence, and arrays while following structural equality rules. For floating-point values, they adhere to Double.equals semantics, treating NaN as equal to itself but distinguishing between -0.0 and 0.0. These functions are currently marked as @ExperimentalStdlibApi.
2Performance Advantages
The allDistinctBy function returns false immediately upon finding a duplicate, avoiding unnecessary processing of entire collections. Similarly, allEqualBy checks if all elements produce identical values through a selector function. This eliminates the need for verbose patterns like map-distinct-size comparisons. The functions address long-standing feature requests from the Kotlin community, with corresponding issues tracked in YouTrack for years before their implementation in the standard library.
3Context-Sensitive Resolution
Introduced experimentally in Kotlin 2.2, context-sensitive resolution allows omitting type qualifiers in when expressions when the compiler can infer the type. This reduces repetition in enum-heavy code, making sealed hierarchies and object declarations more concise. The feature works by leveraging available type information to resolve unqualified names automatically. However, it requires explicit compiler flag -Xcontext-sensitive-resolution to enable.
4Syntax Improvements
With context-sensitive resolution, enum entries in when expressions no longer need full qualification. For example, Problem.CONNECTION can be written as just CONNECTION when the expected type is known. This works for enum entries, sealed class members, and object declarations. The improvement particularly benefits codebases with numerous when expressions, reducing visual noise while maintaining type safety through compiler inference.
5Experimental Status Considerations
Both features currently carry experimental status. allDistinctBy/allEqualBy require opting into experimental standard library APIs, while context-sensitive resolution needs a compiler flag. The experimental nature means syntax and behavior might change in future versions. Context-sensitive resolution also faces potential ambiguity issues if multiple declarations with identical names exist in scope, though the compiler provides warnings for some cases.
6Practical Adoption Advice
For production code, developers should carefully evaluate whether to adopt these experimental features. allDistinctBy/allEqualBy are relatively safe additions for projects already using experimental APIs. Context-sensitive resolution is better suited for side projects or codebases comfortable with experimental compiler features. The Kotlin team recommends treating these as experimental rather than assuming finalized syntax.
7Impact on Code Quality
These additions continue Kotlin's tradition of eliminating boilerplate. While they don't fundamentally change application architecture, they make common patterns more concise. The collection utilities offer performance benefits for uniqueness checks, and context-sensitive resolution improves readability in type-heavy code. Together they represent Kotlin's focus on developer experience through incremental improvements.
8Community Response
The Kotlin community has welcomed these features as quality-of-life improvements addressing real pain points. The allDistinctBy/allEqualBy functions fulfill long-standing requests, while context-sensitive resolution tackles enum verbosity. Their experimental status allows for community feedback before stabilization. These additions demonstrate JetBrains' commitment to evolving Kotlin based on developer needs while maintaining backward compatibility.