Stream handle method#1238
Conversation
There was a problem hiding this comment.
it's great.
The definition of handler in
kyo/kyo-kernel/shared/src/main/scala/kyo/kernel/Pending.scala
Lines 181 to 184 in d90f29a
use a call-by-name
f:(=> <[A, S]) => B
It's not possible to do the same?
val streamOfResult = stream.handle(Abort.run, ???)with something like
def handle[V1, S1](
f: (=> Unit < S) => (Unit < (Emit[Chunk[V1]] & S1))
)(using Frame): Stream[V | V1, S1]|
I don't have the logic behind this call-by-name, but there might be something to do with inlining and allocations. a potential signature could be: def handle[V1 >: V, S1](
f: (=> Unit < S) => (Unit < (Emit[Chunk[V1]] & S1))
)(using Frame, Tag[Emit[Chunk[V1]]], Tag[Emit[Chunk[V]]]): Stream[V1, S1]I have a case for a stream.handle(
_.fold(
onSuccess = () => Chunk.empty,
onFail = _ => Chunk(-1),
onPanic = _ => Chunk(-2)
),
_.map(chunk => Emit.value(chunk))
) |
The by-name param was introduced to allow Regarding
We recommend |
|
I updated |
Addresses discussion of resource handling on discord
Problem
Handling stream effects is currently a little awkward. You have to apply a handler to the stream's
emiteffect and then construct a new Stream from the transformed effect. To handle resources within the scope of a stream, for instance, you would do:Solution
This PR adds a
handlemethod toStreamsimilar to thehandlemethod onPending: it accepts one or more functions transforming the underlying emit effect, and constructs a new stream using the transformed effect. You can handle multiple effects as follows:The functions are expected to evaluate to a
Emit[Chunked[V1]]effect whereV1need not be the same as the originalVtype. It does not have to evaluate toUnit < ..., as the implementation calls.unitto ensure it will be a valid streaming effect. This means you can passAbort.run[String](_)without having to worry about the fact that the transformed effect will evaluate toResult[Unit, String].Notes