All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.net.codinux.csv.Closeable.kt Maven / Gradle / Ivy

Go to download

Port of FastCSV, an ultra fast and simple RFC 4180 compliant CSV library, for Kotlin Multiplatform

The newest version!
package net.codinux.csv

import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract

interface Closeable {

  @Throws(IOException::class)
  fun close()

}

@OptIn(ExperimentalContracts::class)
inline fun  T.use(block: (T) -> R): R {
  contract {
    callsInPlace(block, InvocationKind.EXACTLY_ONCE)
  }

  var exception: Throwable? = null
  try {
    return block(this)
  } catch (e: Throwable) {
    exception = e
    throw e
  } finally {
    when {
      this == null -> {}
      exception == null -> close()
      else ->
        try {
          close()
        } catch (closeException: Throwable) {
          // cause.addSuppressed(closeException) // ignored here
        }
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy