kotlin-types-value-class

Guides choosing @JvmInline value class over data class for single-field Kotlin types.

Updated Jul 1, 2026
One-click install
npx skills add https://github.com/w0lzard/Wolzard-s-Marketplace --skill kotlin-types-value-class-w0lzard
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: kotlin-types-value-class
Source: https://github.com/w0lzard/Wolzard-s-Marketplace/tree/main/plugins/personal-skills/skills/kotlin-types-value-class
Command: npx skills add https://github.com/w0lzard/Wolzard-s-Marketplace --skill kotlin-types-value-class-w0lzard

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Kotlin developers often wrap single values in data classes, paying unnecessary allocation costs, or use raw primitives where domain types would prevent mixing up values like UserId and String. This Skill provides decision rules for when @JvmInline value class is the right choice, including its Compose stability benefits. ## Core Features & Use Cases - Decision flow: A table mapping situations (single field with domain meaning, multiple fields, custom equality needs) to the right type declaration. - Compose stability guidance: Explains that value classes wrapping stable types are treated as Stable by the Compose compiler, improving skippability at UI boundaries. - Gotcha coverage: Documents autoboxing in nullable/generic positions, serialization contract changes with kotlinx.serialization, and the lack of copy()/componentN(). - Use Case: While reviewing a pull request, you spot data class UserId(val value: String) and replace it with @JvmInline value class UserId(val value: String) to remove the wrapper allocation while keeping type safety. ## Quick Start Review this Kotlin file and replace any single-field data classes that carry domain meaning with @JvmInline value classes, flagging any cases where autoboxing or serialization would be affected.

Frequently Asked Questions about kotlin-types-value-class

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
When should I use @JvmInline value class instead of data class in Kotlin?

Use @JvmInline value class when a type wraps a single field with domain meaning, such as UserId or EmailAddress. Use a data class when you need multiple fields, custom equals/hashCode, copy(), or destructuring, since value classes delegate equality to the underlying type.

How do value classes affect Compose stability and skippability?

The Compose compiler treats a value class as Stable when its underlying type is stable, such as primitives or String. Replacing single-field data classes with value classes at UI boundaries avoids unstable parameter warnings and improves composable skippability without @Immutable annotations.

Does kotlinx.serialization work with Kotlin value classes?

Yes, but the JSON contract changes: a @Serializable value class serializes as the underlying value, while a data class serializes as an object with a named field. Replacing a single-field data class with a value class is a breaking change for existing API consumers.

Why does a Kotlin value class still allocate memory sometimes?

Value classes are unboxed at compile time but boxed when used as nullable types, generic type arguments, or vararg parameters. In hot paths these allocations can matter, so measure before converting heavily generic or nullable usages.

What are the limitations of Kotlin value classes?

Value classes cannot have init blocks, backing fields, lateinit, delegated properties, custom equals/hashCode, copy(), or componentN() destructuring. From Java they appear as the underlying type, so Java callers bypass the type-safety wrapper.