hn.today

Value classes in Kotlin 2.5.0 and beyond

blog.jetbrains.com7 points0 comments
Screenshot of Value classes in Kotlin 2.5.0 and beyond

Kotlin 2.5.0 introduces full value classes in Alpha as a way to make immutable, data-oriented programming as convenient as data classes while enforcing stronger value semantics and potentially improving performance. A full value class can contain any number of primary properties, but its entire state must come from those properties: additional properties may be computed, not stored. The compiler generates equality, hashing, and string conversion, but not data classes’ copy and component functions. Instead, value classes support name-based destructuring, and a proposed “copy properties” feature for ergonomic immutable updates is planned for an Alpha release in Kotlin 2.6.0. Migration warnings for classes marked as future value classes are planned from Kotlin 2.5.20.

The feature works across Kotlin platforms and JDK versions, while an optional compilation mode can map value classes to JVM value classes using Project Valhalla, which requires JDK 28 or later. Kotlin is also expanding existing single-property inline value classes: from 2.5.20, the inline modifier can replace @JvmInline, with consistent requirements across platforms. Because value classes have no object identity, referential equality and synchronization are inappropriate; compiler diagnostics aim to catch these and other pitfalls, including suspicious equality comparisons and potentially unsafe lambda captures. The features remain experimental, with breaking changes possible, and broader optimizations and interoperability improvements for Native, JavaScript, WebAssembly, Android, Swift, and C remain future work.

Read on blog.jetbrains.com0 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

Show HN: Rust Commander

Show HN: Rust Commander

Rust Commander is a Rust terminal file manager modeled on Midnight Commander, with dual-pane navigation, a persistent shell, archive and remote browsing, and background file operations. Version 0.3.1 adds file-change alerts, viewer copy controls, customizable skins, and faster SFTP previews and copying. (github.com)

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.