0

I don't quite understand the user-level design of ZIO's ZStream when writing to a queue:

  val queue: RIO[Scope, Dequeue[Take[Nothing, Int]]] =
    ZStream(1, 2).toQueue()

... emitting (for example) Take(Success(Chunk(1)))

I understand that, at a low level, something like this is necessary as a way of expressing completions & errors as data types. But this seems a bit cumbersome at the user level. Is there some way I'm supposed to be unwrapping these into plain elements?

It looks like Take#done might be part of the puzzle:

  def done[R](implicit trace: Trace): ZIO[R, Option[E], Chunk[A]] =
    ZIO.done(exit)

But I feel like there's probably some utility lying around somewhere, which I'm seeing, which knows how to unwrap the resulting Option[E] into an E, and the Chunk[A] so that each result-consumption operation can just deal with a single A.

Tim W
  • 1
  • 1
  • I'm not sure to understand what data type you have as input and how you want to process it. Maybe you could provide a pseudo code of what you'd like to have. – Gaël J Jul 20 '23 at 17:43
  • Thank you, Gaël J. For the code example above, I'd like to know the idiomatic way to simply read an ```Int```. (Or any other type ```A``` which is being emitted wrapped in ```Take[_, A]```) – Tim W Jul 21 '23 at 13:58
  • Put another way, I would have simply expected there to be something like ```.toQueue()``` which yielded a ```Dequeue[..., Int]``` -- or a utility for more easily unwrapping a ```Take```, much like what ```.flattenExitOption``` does for streams. – Tim W Jul 21 '23 at 14:11

1 Answers1

0

After a bit of research and experimentation I've learned some things. These might not be optimal (will leave this open for a while to see if others add helpful answers), but they seem okay for the moment.

One solution is to use ZStream#toQueueOfElements(), which instead yields Dequeue[Exit[Option[Nothing], A]]. This isn't quite like being able to do an .await on a Dequeue[A], but it's a little bit better -- for an element (a) you can do:

a match {
  case Exit.Success(value) => ???
  case Exit.Failure(cause) => ???
}
Tim W
  • 1
  • 1