the "use" keyword for multiple resources

This page summarizes the projects mentioned and recommended in the original post on /r/Kotlin

Our great sponsors
  • WorkOS - The modern identity platform for B2B SaaS
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • SaaSHub - Software Alternatives and Reviews
  • Sprinkler

    A non-intrusive set of extensions sprinkled over the Kotlin standard library.

  • KotlinUsings

    An implementation of the using-Keyword from C# as inline function for Kotlin.

  • KotlinUsings, first google result: https://github.com/FelixEngl/KotlinUsings

  • WorkOS

    The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.

    WorkOS logo
  • Gradle

    Adaptable, fast automation for all

  • import java.io.Closeable import java.io.File import java.io.OutputStream import java.math.BigInteger import java.security.DigestOutputStream import java.security.MessageDigest /** Create MD5 checksum for [file] */ fun md5(file: File): String { val md5 = MessageDigest.getInstance("MD5") // create an inline object to hold the resources val resources = object : Closeable { // implement Closeable so `use {}` is available // define the resources as fields val fileStream = file.inputStream().buffered() val digestStream = DigestOutputStream(OutputStream.nullOutputStream(), md5) // manually close the resources override fun close() { fileStream.close() digestStream.close() } } resources.useToRun { // the resources are available as properties of the `resources` object fileStream.transferTo(digestStream) } return "%032x".format(BigInteger(1, md5.digest())) } /** Combined [use] and [run] */ // copied from Gradle Kotlin DSL util // https://github.com/gradle/gradle/blob/1c0c3ddb71f67a303b30c5c5c2e2f75fff83703f/subprojects/kotlin-dsl/src/main/kotlin/org/gradle/kotlin/dsl/support/IO.kt#L26-L27 inline fun T.useToRun(action: T.() -> U): U = use { run(action) }

  • defer-kt

    Go-inspired defer-based resource management

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts