Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.
/ naked Public archive
generated from bnorm/kotlin-ir-plugin-template

Kotlin Compiler plugin which unwraps value classes in compile time

Notifications You must be signed in to change notification settings

jvmusin/naked

Repository files navigation

Naked Kotlin Compiler Plugin

The plugin which lets you easily inline your value classes with a single annotation:

import io.github.jvmusin.naked.Naked

@JvmInline
value class Wrapper(val data: String)

@JvmInline
@Naked
value class InlinedWrapper(val data: String)

fun main() {
    println("Hello, ${Wrapper("World")}!") // Prints "Hello, Wrapper(data=World)!"
    println("Hello, ${InlinedWrapper("World")}!") // Prints "Hello, World!"
}

Motivation

The reason you may want to inline value classes entirely is to avoid unnecessary boxing when value classes are used as generic or nullable types.

Applying @Naked annotation to the value class causes all the real objects of this type to be replaced with a wrapped value – with a string in the example above.

Usage

  1. Register the plugin io.github.jvmusin.naked in the plugins section of your project's build.gradle.kts file:

    plugins {
        ... your other plugins
        id("io.github.jvmusin.naked") version "0.0.1"
    }
    
  2. Annotate your value classes with @io.github.jvmusin.naked.Naked

  3. That's it! All usages of the annotated class will be replaced with usages of the wrapped value!

You can disable the plugin using naked extension in build.gradle.kts file:

naked {
    enabled = false
}

About

Kotlin Compiler plugin which unwraps value classes in compile time

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages