• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3  */
4 
5 package kotlinx.coroutines
6 
7 public class Try<out T> private constructor(private val _value: Any?) {
8     private class Fail(val exception: Throwable) {
toStringnull9         override fun toString(): String = "Failure[$exception]"
10     }
11 
12     public companion object {
13         public operator fun <T> invoke(block: () -> T): Try<T> =
14                 try {
15                     Success(block())
16                 } catch(e: Throwable) {
17                     Failure<T>(e)
18                 }
19         public fun <T> Success(value: T) = Try<T>(value)
20         public fun <T> Failure(exception: Throwable) = Try<T>(Fail(exception))
21     }
22 
23     @Suppress("UNCHECKED_CAST")
24     public val value: T get() = if (_value is Fail) throw _value.exception else _value as T
25 
26     public val exception: Throwable? get() = (_value as? Fail)?.exception
27 
toStringnull28     override fun toString(): String = _value.toString()
29 }
30