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

commonTest.Try.kt Maven / Gradle / Ivy

There is a newer version: 1.9.0
Show newest version
/*
 * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.coroutines

public class Try private constructor(private val _value: Any?) {
    private class Fail(val exception: Throwable) {
        override fun toString(): String = "Failure[$exception]"
    }

    public companion object {
        public operator fun  invoke(block: () -> T): Try =
                try {
                    Success(block())
                } catch(e: Throwable) {
                    Failure(e)
                }
        public fun  Success(value: T) = Try(value)
        public fun  Failure(exception: Throwable) = Try(Fail(exception))
    }

    @Suppress("UNCHECKED_CAST")
    public val value: T get() = if (_value is Fail) throw _value.exception else _value as T

    public val exception: Throwable? get() = (_value as? Fail)?.exception

    override fun toString(): String = _value.toString()
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy