Skip to content
Logo

Generator syntax

@E.gen turns a generator function into a factory of effects: the interpreter runs each yielded effect and sends its success value back into the generator, and the generator's return value becomes the effect's success value. Write x = yield from effect, not x = yield effectEffect.__iter__ is typed so yield from gives x the effect's success type, while a bare yield types as Any.

@E.
def (: ) -> E.[, ]:
     = yield from E.(20)  # a: int — yield from types the sent-back value
 
    if  < 0:
        yield from E.(
            (msg="negative")
        )  # OopsError joins the error channel
    return  + 
 
 
E.((22))  # 42

A failing yielded effect abandons the generator, so try/except around a yield never observes effect failures — use catch or catch_all on the resulting effect instead.

More examples: test_gen.py.