Skip to main content
Version: 2.0.x

Creating Channels

ZChannel have several constructors and also built-in channels, where suitable to create more complex channels.

Without further ado, let's learn them one by one:


Creates a channel that succeeds with a given done value, e.g. ZChannel.succeed(42):


val channel: ZChannel[Any, Any, Any, Any, Nothing, Nothing, Int] =

This channel doesn't produce any data but succeeds with a done value of type Int. Let's try to runCollect this channel and see what happens:


The output of the runCollect operation is a tuple of two elements: the first is a chunk of data that the channel produced, and the second is the done value. Because this channel doesn't produce any data, the first element is an empty chunk, but it has a 42 as the done value in the second element.

Creates a channel that fails with a given error, e.g. Exception("error")):


val channel: ZChannel[Any, Any, Any, Any, Exception, Nothing, Nothing] = Exception("error"))


Create a channel that writes given elements to the output port:

import zio._

// Output: (Chunk(1),())

ZChannel.writeAll(1, 2, 3).runCollect.debug
// Output: (Chunk(1,2,3),())

ZChannel.writeChunk(Chunk(1, 2, 3)).runCollect.debug
// Output: (Chunk(1,2,3),())*

Create a channel that reads elements from the input port and returns that as a done value:

Let's start with the simplest read operation,


val read: ZChannel[Any, Any, Int, Any, None.type, Nothing, Int] =[Int]

To test this channel, we can create a writer channel and then pipe that to the reader channel:


val read =[Int]

(ZChannel.write(1) >>> read).runCollect.debug
// Output: (Chunk(0),1)

In the above example, the writer channel writes the value 1 to the output port, and the reader channel reads the value from the input port and then returns it as a done value.

If we compose multiple read operations, we can read more values from the input port:


val read =[Int]

(ZChannel.writeAll(1, 2, 3) >>> (read *> read)).runCollect.debug
// Output: (Chunk(),2)

(ZChannel.writeAll(1, 2, 3) >>> (read *> read *> read)).runCollect.debug
// Output: (Chunk(),3)

Another useful read operation is ZChannel.readWith. Using this operator, after reading a value from the input port, instead of returning it as a done value, we have the ability to pass the input value to another channel.