<A, In, L, E, R>(self: Sink<A, In, L, E, R>): Channel.Channel<
never,
E,
End<A, L>,
NonEmptyReadonlyArray<In>,
never,
void,
R
>Creates a Channel from a Sink.
Example (Converting a sink to a channel)
import { Sink } from "effect"
// Create a sink and extract its channel
const sink = Sink.succeed(42)
const channel = Sink.toChannel(sink)export const const toChannel: <A, In, L, E, R>(
self: Sink<A, In, L, E, R>
) => Channel.Channel<
never,
E,
End<A, L>,
NonEmptyReadonlyArray<In>,
never,
void,
R
>
Creates a Channel from a Sink.
Example (Converting a sink to a channel)
import { Sink } from "effect"
// Create a sink and extract its channel
const sink = Sink.succeed(42)
const channel = Sink.toChannel(sink)
toChannel = <function (type parameter) A in <A, In, L, E, R>(self: Sink<A, In, L, E, R>): Channel.Channel<never, E, End<A, L>, NonEmptyReadonlyArray<In>, never, void, R>A, function (type parameter) In in <A, In, L, E, R>(self: Sink<A, In, L, E, R>): Channel.Channel<never, E, End<A, L>, NonEmptyReadonlyArray<In>, never, void, R>In, function (type parameter) L in <A, In, L, E, R>(self: Sink<A, In, L, E, R>): Channel.Channel<never, E, End<A, L>, NonEmptyReadonlyArray<In>, never, void, R>L, function (type parameter) E in <A, In, L, E, R>(self: Sink<A, In, L, E, R>): Channel.Channel<never, E, End<A, L>, NonEmptyReadonlyArray<In>, never, void, R>E, function (type parameter) R in <A, In, L, E, R>(self: Sink<A, In, L, E, R>): Channel.Channel<never, E, End<A, L>, NonEmptyReadonlyArray<In>, never, void, R>R>(
self: Sink<A, In, L, E, R>(parameter) self: {
transform: (upstream: Pull.Pull<NonEmptyReadonlyArray<In>, never, void>, scope: Scope.Scope) => Effect.Effect<End<A, L>, E, R>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
self: interface Sink<out A, in In = unknown, out L = never, out E = never, out R = never>A Sink<A, In, L, E, R> is used to consume elements produced by a Stream.
You can think of a sink as a function that will consume a variable amount of
In elements (could be 0, 1, or many), might fail with an error of type E,
and will eventually yield a value of type A together with a remainder of
type L (i.e. any leftovers).
Example (Running a sink with a stream)
import { Effect, Sink, Stream } from "effect"
// Create a simple sink that always succeeds with a value
const sink: Sink.Sink<number> = Sink.succeed(42)
// Use the sink to consume a stream
const stream = Stream.make(1, 2, 3)
const program = Stream.run(stream, sink)
Effect.runPromise(program).then(console.log)
// Output: 42
Namespace containing types and interfaces for Sink variance and type relationships.
Sink<function (type parameter) A in <A, In, L, E, R>(self: Sink<A, In, L, E, R>): Channel.Channel<never, E, End<A, L>, NonEmptyReadonlyArray<In>, never, void, R>A, function (type parameter) In in <A, In, L, E, R>(self: Sink<A, In, L, E, R>): Channel.Channel<never, E, End<A, L>, NonEmptyReadonlyArray<In>, never, void, R>In, function (type parameter) L in <A, In, L, E, R>(self: Sink<A, In, L, E, R>): Channel.Channel<never, E, End<A, L>, NonEmptyReadonlyArray<In>, never, void, R>L, function (type parameter) E in <A, In, L, E, R>(self: Sink<A, In, L, E, R>): Channel.Channel<never, E, End<A, L>, NonEmptyReadonlyArray<In>, never, void, R>E, function (type parameter) R in <A, In, L, E, R>(self: Sink<A, In, L, E, R>): Channel.Channel<never, E, End<A, L>, NonEmptyReadonlyArray<In>, never, void, R>R>
): import ChannelChannel.interface Channel<out OutElem, out OutErr = never, out OutDone = void, in InElem = unknown, in InErr = unknown, in InDone = unknown, out Env = never>A Channel is a nexus of I/O operations, which supports both reading and
writing. A channel may read values of type InElem and write values of type
OutElem. When the channel finishes, it yields a value of type OutDone. A
channel may fail with a value of type OutErr.
Details
Channels are the foundation of Streams: both streams and sinks are built on
channels. Most users shouldn't have to use channels directly, as streams and
sinks are much more convenient and cover all common use cases. However, when
adding new stream and sink operators, or doing something highly specialized,
it may be useful to use channels directly.
Channels compose in a variety of ways:
- Piping: One channel can be piped to another channel, assuming the
input type of the second is the same as the output type of the first.
- Sequencing: The terminal value of one channel can be used to create
another channel, and both the first channel and the function that makes
the second channel can be composed into a channel.
- Concatenating: The output of one channel can be used to create other
channels, which are all concatenated together. The first channel and the
function that makes the other channels can be composed into a channel.
Example (Typing channels)
import type { Channel } from "effect"
// A channel that outputs numbers and requires no environment
type NumberChannel = Channel.Channel<number>
// A channel that outputs strings, can fail with Error, completes with boolean
type StringChannel = Channel.Channel<string, Error, boolean>
// A channel with all type parameters specified
type FullChannel = Channel.Channel<
string, // OutElem - output elements
Error, // OutErr - output errors
number, // OutDone - completion value
number, // InElem - input elements
string, // InErr - input errors
boolean, // InDone - input completion
{ db: string } // Env - required environment
>
Channel<never, function (type parameter) E in <A, In, L, E, R>(self: Sink<A, In, L, E, R>): Channel.Channel<never, E, End<A, L>, NonEmptyReadonlyArray<In>, never, void, R>E, type End<A, L = never> = readonly [
value: A,
leftover?: readonly [L, ...L[]] | undefined
]
Tuple returned when a Sink finishes.
Details
The first element is the sink result. The optional second element contains a
non-empty array of leftover input that was pulled but not consumed.
End<function (type parameter) A in <A, In, L, E, R>(self: Sink<A, In, L, E, R>): Channel.Channel<never, E, End<A, L>, NonEmptyReadonlyArray<In>, never, void, R>A, function (type parameter) L in <A, In, L, E, R>(self: Sink<A, In, L, E, R>): Channel.Channel<never, E, End<A, L>, NonEmptyReadonlyArray<In>, never, void, R>L>, type NonEmptyReadonlyArray<A> = readonly [A, ...A[]]A readonly array guaranteed to have at least one element.
When to use
Use when non-emptiness must be tracked at the type level while preventing mutation.
Many Array module functions accept or return this type.
Example (Typing a non-empty array)
import type { Array } from "effect"
const nonEmpty: Array.NonEmptyReadonlyArray<number> = [1, 2, 3]
const head: number = nonEmpty[0] // guaranteed to exist
NonEmptyReadonlyArray<function (type parameter) In in <A, In, L, E, R>(self: Sink<A, In, L, E, R>): Channel.Channel<never, E, End<A, L>, NonEmptyReadonlyArray<In>, never, void, R>In>, never, void, function (type parameter) R in <A, In, L, E, R>(self: Sink<A, In, L, E, R>): Channel.Channel<never, E, End<A, L>, NonEmptyReadonlyArray<In>, never, void, R>R> =>
import ChannelChannel.const fromTransform: <
OutElem,
OutErr,
OutDone,
InElem,
InErr,
InDone,
EX,
EnvX,
Env
>(
transform: (
upstream: Pull.Pull<InElem, InErr, InDone>,
scope: Scope.Scope
) => Effect.Effect<
Pull.Pull<OutElem, OutErr, OutDone, EnvX>,
EX,
Env
>
) => Channel<
OutElem,
Pull.ExcludeDone<OutErr> | EX,
OutDone,
InElem,
InErr,
InDone,
Env | EnvX
>
Creates a Channel from a transformation function that operates on upstream pulls.
Example (Creating channels from transforms)
import { Channel, Effect } from "effect"
const channel = Channel.fromTransform((upstream, scope) =>
Effect.succeed(upstream)
)
fromTransform((upstream: Pull.Pull<
readonly [In, ...In[]],
never,
void,
never
>
(parameter) upstream: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
upstream, scope: Scope.Scope(parameter) scope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
scope) =>
import EffectEffect.const succeed: <A>(value: A) => Effect<A>Creates an Effect that always succeeds with a given value.
When to use
Use when an effect should complete successfully with a specific value without any errors
or external dependencies.
Example (Creating a successful effect)
import { Effect } from "effect"
// Creating an effect that represents a successful scenario
//
// ┌─── Effect<number, never, never>
// ▼
const success = Effect.succeed(42)
succeed(import EffectEffect.const flatMap: {
<A, B, E1, R1>(
f: (a: A) => Effect<B, E1, R1>
): <E, R>(
self: Effect<A, E, R>
) => Effect<B, E1 | E, R1 | R>
<A, E, R, B, E1, R1>(
self: Effect<A, E, R>,
f: (a: A) => Effect<B, E1, R1>
): Effect<B, E | E1, R | R1>
}
flatMap(
self: Sink<A, In, L, E, R>(parameter) self: {
transform: (upstream: Pull.Pull<NonEmptyReadonlyArray<In>, never, void>, scope: Scope.Scope) => Effect.Effect<End<A, L>, E, R>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
self.Sink<A, In, L, E, R>.transform: (upstream: Pull.Pull<NonEmptyReadonlyArray<In>, never, void>, scope: Scope.Scope) => Effect.Effect<End<A, L>, E, R>transform(upstream: Pull.Pull<
readonly [In, ...In[]],
never,
void,
never
>
(parameter) upstream: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
upstream, scope: Scope.Scope(parameter) scope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
scope),
import CauseCause.const done: <A = void>(
value?: A
) => Effect.Effect<never, Done<A>>
Creates an Effect that fails with a Done error. Shorthand for
Effect.fail(Cause.Done(value)).
When to use
Use when you model stream or queue completion through the error channel.
Example (Failing with Done)
import { Cause, Effect } from "effect"
const program = Cause.done("finished")
Effect.runPromiseExit(program).then((exit) => {
console.log(exit._tag) // "Failure"
})
done
))
)