#How to have async generators that can be assumed arbitrary amount of times?

1 messages · Page 1 of 1 (latest)

hardy gull
#

Basically I'm trying to design a function (generator of values) that can be suspended and needs to be resumed in order to produce new values. Function itself decides when the values are exhausted. But how do I find out at calling site that frame should no longer be resumed? Basically I'm having the same question as this post: https://stackoverflow.com/questions/65975848/in-zig-how-do-you-tell-if-a-frame-represents-a-fully-executed-function

compact notch
#

I don't believe there's currently a good way to do this; what I'd suggest is having its "returned" value be a ?T and on the last resume set it to null to signal that no more values are available and the generator shouldn't be called into again.

hardy gull
#

thanks for the tip! but still, it's a shame, because my "generator" will actually shuffle elements in place in some slice. but please correct me if i'm wrong - there's no nice/clean way to return values from async functions, so I must use either pointers or global values?

rich peak
#

what if you have a separate iterator that you call to actually retrieve the values (returning ?T)

mystic mountain
#

its meant to be used for coroutines where:
"routine" = function: runs code and reports a return value when done
"coroutine" = function that can be suspended and resumed

compact notch
#

yeah - the normal solution here would be to make an actual iterator struct with regular methods instead. there aren't many cases where a single generator-style function is significantly nicer

celest lotus
hardy gull
#

thank you for the explanation, I'll need to refactor my code then.