An open API service indexing awesome lists of open source software.

https://github.com/karmakrafts/rakii

Structured RAII with error handling for Kotlin Multiplatform.
https://github.com/karmakrafts/rakii

gradle gradle-plugin kotlin kotlin-android kotlin-compiler-plugin kotlin-js kotlin-jvm kotlin-library kotlin-multiplatform kotlin-native library

Last synced: 3 months ago
JSON representation

Structured RAII with error handling for Kotlin Multiplatform.

Awesome Lists containing this project

README

        

# RAkII

[![](https://git.karmakrafts.dev/kk/rakii/badges/master/pipeline.svg)](https://git.karmakrafts.dev/kk/rakii/-/pipelines)
[![](https://img.shields.io/maven-metadata/v?metadataUrl=https%3A%2F%2Fcentral.sonatype.com%2Fpublish%2Fstaging%2Fmaven2%2Fdev%2Fkarmakrafts%2Frakii%2Frakii-runtime%2Fmaven-metadata.xml
)](https://git.karmakrafts.dev/kk/rakii/-/packages)
[![](https://img.shields.io/maven-metadata/v?metadataUrl=https%3A%2F%2Fcentral.sonatype.com%2Frepository%2Fmaven-snapshots%2Fdev%2Fkarmakrafts%2Frakii%2Frakii-runtime%2Fmaven-metadata.xml
)](https://git.karmakrafts.dev/kk/rakii/-/packages)

RAkII (or Kotlin RAII) is a lightweight runtime library and compiler plugin
which allows using structured RAII with support for error handling in Kotlin Multiplatform.

RAII is a concept commonly known from native languages such as C++ and Rust,
used for managing the lifetime of memory implicitly.
However, since Cleaners are not suitable for managing micro-allocations in
a granular manner in Kotlin, this library can be employed to reduce potential for
common errors, resource leaks and double-frees.

### How to configure it

Simply add the required repositories to your build configuration, apply the
Gradle plugin and add a dependency on the runtime:

In your `settings.gradle.kts`:

```kotlin
pluginManagement {
repositories {
// Snapshots are available from the Karma Krafts repository or Maven Central Snapshots
maven("https://files.karmakrafts.dev/maven")
maven("https://central.sonatype.com/repository/maven-snapshots")
// Releases are mirrored to the central M2 repository
mavenCentral()
}
}

dependencyResolutionManagement {
repositories {
// Snapshots are available from the Karma Krafts repository or Maven Central Snapshots
maven("https://files.karmakrafts.dev/maven")
maven("https://central.sonatype.com/repository/maven-snapshots")
// Releases are mirrored to the central M2 repository
mavenCentral()
}
}
```

In your `build.gradle.kts`::

```kotlin
plugins {
id("dev.karmakrafts.rakii.rakii-gradle-plugin") version ""
}

kotlin {
sourceSets {
commonMain {
dependencies {
implementation("dev.karmakrafts.rakii:rakii-runtime:")
}
}
}
}
```

**Note: It is recommended to use Gradle version catalogs which were omitted here for simplification.**

### How to use it

```kotlin
import dev.karmakrafts.rakii.Drop

class Foo : Drop {
fun doTheThing() {
println("I am doing the thing!")
}
}

class Bar : Drop {
val foo1 by dropping(::Foo)
// When the initialization of foo2 fails, foo1 will be dropped immediately
val foo2 by dropping(::Foo).dropOnAnyError(Bar::foo1)

fun doTheThings() {
foo1.doTheThing()
foo2.doTheThing()
}
}

fun main() {
Bar().use {
// Normally use Bar and its contained Foo instance
// Once the use-scope ends, Foo is dropped and Bar is dropped afterward
it.doTheThings()
}
}
```