import { Option } from './Option';
export type Success = Try;
export type Failure = Try;
export declare abstract class Try {
/**
* Evaluates the given `thunk` and returns either a [[Success]],
* in case the evaluation succeeded, or a [[Failure]], in case
* an exception was thrown.
*
* Example:
*
* ```typescript
* let effect = 0
*
* const e = Try.of(() => { effect += 1; return effect })
* e.get() // 1
* ```
*/
static of(thunk: () => T): Try;
/**
* Returns a [[Try]] reference that represents a successful result
* (i.e. wrapped in [[Success]]).
*/
static success(value: T): Try;
/**
* Returns a [[Try]] reference that represents a failure
* (i.e. an exception wrapped in [[Failure]]).
*/
static failure(error: unknown): Try;
private readonly isSuccessTag;
private readonly value;
protected constructor(value: A, tag: 'failure' | 'success');
/**
* Returns `true` if the source is a [[Success]] result,
* or `false` in case it is a [[Failure]].
*/
isSuccess(): this is Success;
/**
* Returns `true` if the source is a [[Failure]],
* or `false` in case it is a [[Success]] result.
*/
isFailure(): this is Failure;
/**
* Returns a Try's successful value if it's a [[Success]] reference,
* otherwise throws an exception if it's a [[Failure]].
*
* WARNING!
*
* This function is partial, the option must be non-empty, otherwise
* a runtime exception will get thrown. Use with care.
*/
get(): A;
/**
* Returns the value from a `Success` or the given `fallback`
* value if this is a `Failure`.
*
* ```typescript
* Success(10).getOrElse(27) // 10
* Failure("error").getOrElse(27) // 27
* ```
*/
getOrElse(fallback: AA): A | AA;
/**
* Returns the value from a `Success` or the value generated
* by a given `thunk` in case this is a `Failure`.
*
* ```typescript
* Success(10).getOrElseL(() => 27) // 10
* Failure("error").getOrElseL(() => 27) // 27
* ```
*/
getOrElseL(thunk: () => AA): A | AA;
/**
* Returns the current value if it's a [[Success]], or
* if the source is a [[Failure]] then return `null`.
*
* ```typescript
* Success(10).orNull() // 10
* Failure("error").orNull() // null
* ```
*
* This can be useful for use-cases such as:
*
* ```typescript
* Try.of(() => dict.user.profile.name).orNull()
* ```
*/
orNull(): A | null;
/**
* Returns the current value if it's a [[Success]], or
* if the source is a [[Failure]] then return `undefined`.
*
* ```typescript
* Success(10).orUndefined() // 10
* Failure("error").orUndefined() // undefined
* ```
*
* This can be useful for use-cases such as:
*
* ```typescript
* Try.of(() => dict.user.profile.name).orUndefined()
* ```
*/
orUndefined(): A | undefined;
/**
* Returns the current value if it's a [[Success]], or if
* the source is a [[Failure]] then return the `fallback`.
*
* ```typescript
* Success(10).orElse(Success(17)) // 10
* Failure("error").orElse(Success(17)) // 17
* ```
*/
orElse(fallback: Try): Try;
/**
* Returns the current value if it's a [[Success]], or if the source
* is a [[Failure]] then return the value generated by the given
* `thunk`.
*
* ```typescript
* Success(10).orElseL(() => Success(17)) // 10
* Failure("error").orElseL(() => Success(17)) // 17
* ```
*/
orElseL(thunk: () => Try): Try;
/**
* Applies the `failure` function to [[Failure]] values, and the
* `success` function to [[Success]] values and returns the result.
*
* ```typescript
* const maybeNum: Try =
* tryParseInt("not a number")
*
* const result: string =
* maybeNum.fold(
* error => `Could not parse string: ${error}`,
* num => `Success: ${num}`
* )
* ```
*/
fold(failure: (error: unknown) => R, success: (a: A) => R): R;
/**
* Returns a [[Failure]] if the source is a [[Success]], but the
* given `p` predicate is not satisfied.
*
* @throws NoSuchElementError in case the predicate doesn't hold
*/
filter(p: (a: A) => a is B): Try;
filter(p: (a: A) => boolean): Try;
/**
* Returns the given function applied to the value if this is
* a [[Success]] or returns `this` if this is a [[Failure]].
*
* This operation is the monadic "bind" operation.
* It can be used to *chain* multiple `Try` references.
*
* ```typescript
* Try.of(() => parse(s1)).flatMap(num1 =>
* Try.of(() => parse(s2)).map(num2 =>
* num1 / num2
* ))
* ```
*/
flatMap(f: (a: A) => Try): Try;
/**
* Returns a `Try` containing the result of applying `f` to
* this option's value, but only if it's a `Success`, or
* returns the current `Failure` without any modifications.
*
* NOTE: this is similar with `flatMap`, except with `map` the
* result of `f` doesn't need to be wrapped in a `Try`.
*
* @param f the mapping function that will transform the value
* of this `Try` if successful.
*
* @return a new `Try` instance containing the value of the
* source mapped by the given function
*/
map(f: (a: A) => B): Try;
/**
* Applies the given function `cb` if this is a [[Success]], otherwise
* returns `void` if this is a [[Failure]].
*/
forEach(cb: (a: A) => void): void;
/**
* Applies the given function `f` if this is a `Failure`, otherwise
* returns `this` if this is a `Success`.
*
* This is like `map` for the exception.
*
* In the following example, if the `user.profile.email` exists,
* then it is returned as a successful value, otherwise
*
* ```typescript
* Try.of(() => user.profile.email).recover(e => {
* // Access error? Default to empty.
* if (e instanceof TypeError) return ""
* throw e // We don't know what it is, rethrow
* })
*
* Note that on rethrow, the error is being caught in `recover` and
* it still returns it as a `Failure(e)`.
* ```
*/
recover(f: (error: unknown) => AA): Try;
/**
* Applies the given function `f` if this is a `Failure`, otherwise
* returns `this` if this is a `Success`.
*
* This is like `map` for the exception.
*
* In the following example, if the `user.profile.email` exists,
* then it is returned as a successful value, otherwise
*
* ```typescript
* Try.of(() => user.profile.email).recover(e => {
* // Access error? Default to empty.
* if (e instanceof TypeError) return ""
* throw e // We don't know what it is, rethrow
* })
*
* Note that on rethrow, the error is being caught in `recover` and
* it still returns it as a `Failure(e)`.
* ```
*/
recoverWith(f: (error: unknown) => Try): Try;
/**
* Transforms the source into an [[Option]].
*
* In case the source is a `Success(v)`, then it gets translated
* into a `Some(v)`. If the source is a `Failure(e)`, then a `None`
* value is returned.
*
* ```typescript
* Success("value").toOption() // Some("value")
* Failure("error").toOption() // None
* ```
*/
toOption(): Option;
equals(that: Try): boolean;
}
/**
* The `Success` data constructor is for building [[Try]] values that
* are successful results of computations, as opposed to [[Failure]].
*/
export declare function Success(value: A): Try;
/**
* The `Failure` data constructor is for building [[Try]] values that
* represent failures, as opposed to [[Success]].
*/
export declare function Failure(error: unknown): Try;