# ResumableFunctions @yield doesn't work even in the simplest IO example

**URL:** <https://discourse.julialang.org/t/resumablefunctions-yield-doesnt-work-even-in-the-simplest-io-example/136713>\
**Category:** Performance\
**Created:** [April 14, 2026, 5:37pm UTC](https://discourse.julialang.org/t/resumablefunctions-yield-doesnt-work-even-in-the-simplest-io-example/136713 "2026-04-14T17:37:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tdm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tdm/32/213999_2.png) [@tdm](https://discourse.julialang.org/u/tdm)\
**Post date:** [April 14, 2026, 5:37pm UTC](https://discourse.julialang.org/t/resumablefunctions-yield-doesnt-work-even-in-the-simplest-io-example/136713/1 "2026-04-14T17:37:52Z")

</div>

This is the simplest IO example (copied from AI) and explicitly supported in the docs:

```julia-auto
@resumable function read_lines(filename)
    io = open(filename)
    try
        for line in eachline(io)
            @yield line
        end
    finally
        close(io)
    end
end

```

Documentation ([GitHub - JuliaDynamics/ResumableFunctions.jl: C# style generators a.k.a. semi-coroutines for Julia. · GitHub](https://github.com/JuliaDynamics/ResumableFunctions.jl)) says: " \* In a `try` block only top level `@yield` statements are allowed."

Error I get: **ERROR: LoadError: syntax: cannot goto label “\_STATE\_1” inside try/catch block**

I guess this might be expected since we have a loop, but that makes ResumableFunctions practically useless for IO iterators.  
Anybody has a better idea?

---

<div class="post-metadata">

**Author:** ![nhz2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nhz2/32/44428_2.png) [@nhz2](https://discourse.julialang.org/u/nhz2)\
**Post date:** [April 14, 2026, 7:13pm UTC](https://discourse.julialang.org/t/resumablefunctions-yield-doesnt-work-even-in-the-simplest-io-example/136713/2 "2026-04-14T19:13:36Z")

</div>

You can create a struct and implement the iteration interface. [Interfaces · The Julia Language](https://docs.julialang.org/en/v1/manual/interfaces/#man-interface-iteration)

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [April 15, 2026, 2:55am UTC](https://discourse.julialang.org/t/resumablefunctions-yield-doesnt-work-even-in-the-simplest-io-example/136713/3 "2026-04-15T02:55:59Z")

</div>

> [@tdm](#):
>
> explicitly supported in the docs

I don’t see an example with `open` or `try`-`finally` in the docs, so it’s not explicitly supported. “Top level” in the caveat means that you can’t have any local scope-introducing blocks, like `for` loops, so I’d be surprised if it were. `open(...) do ...` won’t work because anonymous functions are explicitly unsupported. I don’t see an explicit mention in the other caveats, but it doesn’t do named nested functions or closures either.

> [@nhz2](#):
>
> You can create a struct and implement the iteration interface.

The goal is to _automatically_ generate a state machine from a function definition block with an intuitive loop structure. A scheduled Task `put!`-ing objects into a Channel does that, but the scheduling is unnecessary overhead for iteration. Even if you’re willing to write out `iterate`, it’s not trivial in general to describe and maintain the stack frame in a struct. ResumableFunctions only went so far.
