Kotlin 2.5.0 Introduces Experimental Companion Blocks and Extensions
Kotlin 2.5.0 introduces experimental companion blocks and extensions to provide static-like member definitions that compile directly to static members on the JVM. These features allow developers to define class members for types they do not control, overcoming the structural limitations of traditional companion objects.
Verified State Diff
Impact & Verification Analysis
Kotlin developers targeting the JVM and Kotlin Multiplatform.
This change improves interoperability with Java static members and allows for more flexible code organization by enabling the addition of members to third-party types without inheritance or wrapper classes.
Full Fact Overview
Kotlin 2.5.0 adds experimental support for 'companion blocks' and 'companion extensions'. Companion blocks allow defining members like constants and factory methods directly within a class using a 'companion' block, which compiles to static members on the JVM rather than a separate singleton object. Companion extensions allow adding members to existing classes or interfaces, including those from Java or external libraries, without requiring control over the original source. These features require the compiler flags -Xcompanion-blocks or -Xcompanion-blocks-and-extensions. While companion objects remain fully supported for cases requiring interface implementation, companion blocks are intended to replace most standard companion object use cases.