Effect

abstract class Effect<A>(val thunk: suspend () -> A)

An abstract representation of a computation that may produce a value or fail with an error.

The Effect class encapsulates a suspendable computation (a "thunk") that produces a value of type A. It provides a functional programming interface for transforming, chaining, and handling errors in computations in a type-safe manner. Subclasses, such as IO, implement specific behaviors for executing the computation.

Parameters

A

The type of the value produced by the computation.

thunk

The suspendable computation that produces a value of type A.

See also

Inheritors

Constructors

Link copied to clipboard
constructor(thunk: suspend () -> A)

Types

Link copied to clipboard
interface Companion<E : Effect<*>>

A companion interface for creating instances of Effect subclasses.

Link copied to clipboard
class RunDSL<A>(defaultValueOnFailure: suspend () -> A, successful: suspend (Outcome.Success<A>) -> A = { it.value }, failed: suspend (Outcome.Failure) -> A = { _ -> defaultValueOnFailure() })

A DSL for configuring the behavior of run and runSync methods.

Properties

Link copied to clipboard
val thunk: suspend () -> A

Functions

Link copied to clipboard
abstract fun <B> ap(function: Effect<(A) -> B>): Effect<B>

Applies a function contained in another Effect to the value of this Effect.

Link copied to clipboard
abstract fun <B> flatMap(transform: suspend (A) -> Effect<B>): IO<B>

Chains this Effect with another Effect using the provided transform function.

Link copied to clipboard
abstract fun handleError(recover: suspend (Throwable) -> A): Effect<A>

Handles errors in this Effect by recovering with a default value.

Link copied to clipboard
abstract fun <B> map(transform: suspend (A) -> B): Effect<B>

Transforms the result of this Effect using the provided transform function.

Link copied to clipboard
abstract fun recoverWith(recover: suspend (Throwable) -> Effect<A>): Effect<A>

Handles errors in this Effect by recovering with another Effect.

Link copied to clipboard
open suspend fun run(defaultValueOnFailure: A, block: suspend Effect.RunDSL<A>.() -> Unit = {}): A

Executes the computation asynchronously, returning a default value on failure.

open suspend fun run(defaultValueOnFailure: suspend () -> A, block: suspend Effect.RunDSL<A>.() -> Unit = {}): A

Executes the computation asynchronously, using a suspendable default value provider on failure.

Link copied to clipboard
open fun runSync(defaultValueOnFailure: A, block: suspend Effect.RunDSL<A>.() -> Unit = {}): A

Executes the computation synchronously, returning a default value on failure.

open fun runSync(defaultValueOnFailure: suspend () -> A, block: suspend Effect.RunDSL<A>.() -> Unit = {}): A

Executes the computation synchronously, using a suspendable default value provider on failure.

Link copied to clipboard
abstract fun void(): Effect<Unit>

Converts this Effect into an Effect that produces a Unit value.