JetbrainsDaria VoroninaAnnouncement
The Companions to Come
Summary
JetBrains is extending Kotlin’s companion mechanism with experimental companion blocks and companion extensions in Kotlin 2.5.0. Companion blocks replace the object keyword with a companion block, compiling members as static members on the JVM. Companion extensions allow adding class-level members to any type, even types the developer does not control. These features improve multiplatform interoperability by enabling expected companion block members to be actualized using Java static members. Companion objects remain fully supported and are still required when the companion must implement an interface, but companion blocks are recommended for new code.
Context
JetBrains is extending Kotlin’s companion mechanism to improve multiplatform interoperability and enable new code patterns. Companion objects remain fully supported, but companion blocks are recommended for new code. Companion objects are still needed when the companion must implement an interface.
Approach / What changed
Kotlin 2.5.0 introduces experimental companion blocks and companion extensions. Companion blocks replace the object keyword inside a class with a companion block, compiling members as static members on the JVM. Companion extensions allow adding new class-level members to any type, even types the developer does not control, using the syntax companion fun Type.member() . Both features require compiler flags: -Xcompanion-blocks for blocks, and -Xcompanion-blocks-and-extensions for extensions. Libraries using companion extensions produce pre-release binaries and cannot be consumed as dependencies until the feature is stable.
Takeaways
Companion blocks compile members as static members on the JVM, enabling expected companion block members in Kotlin Multiplatform to be actualized using Java static members.
Companion extensions allow adding class-level members to any type, including Java classes and types without a companion object, using the syntax companion fun Type.member() .
Libraries using companion extensions produce pre-release binaries and cannot be consumed as dependencies until the feature is stable; this restriction does not apply to libraries that only expose companion blocks.
FAQ
When should I still use companion objects instead of companion blocks?
Companion objects are still needed when the companion must implement a particular interface, because companion objects compile to a full-fledged class. Companion blocks do not support interface implementation.
Can companion extensions be added to Java classes or types without a companion object?
Yes, you can define companion extensions for any class or interface, whether it comes from Kotlin or Java, and whether it has a companion object or not. This allows adding class-level members to types you do not control.
Can a library using companion extensions be consumed as a dependency before the feature is stable?
Libraries that use companion extensions produce pre-release binaries, so they cannot be consumed as dependencies until the feature becomes stable. This restriction does not apply to libraries that only expose companion blocks, though consumers must still enable the companion blocks feature.
Source and freshness
- Source
- blog.jetbrains.com
- Published
- Added to Yomu
Summaries are generated from the original by a language model. Check the original before relying on it.
Keep reading
Related reading
The Good Stuff
Paid picks we use ourselves. Links carry a referral code — a subscription through one earns us a commission at no extra cost to you.
The best engineering reads, once a week
One useful email. Unsubscribe anytime.