lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 16 July 2018 at 22:21, Pierre Chapuis <catwell@archlinux.us> wrote:
> On Mon, Jul 16, 2018, at 03:17, Daurnimator wrote:
>
>> What doesn't nicely translate to lua is what to do in the `yield` case:
>>
>> local function foo()
>>     with bar do
>>         coroutine.yield(baz) -- maybe baz contains something based on `bar`
>>     end
>> end
>> local f = coroutine.wrap(foo)
>> f()
>> if xyz then -- only *maybe* does the coroutine get resumed
>>     f()
>> end
>
> I was unsure how that was dealt with in Python. This code:
>
>     class Foo(object):
>
>         def __init__(self):
>             pass
>
>         def __enter__(self):
>             print("enter")
>             return self
>
>         def __exit__(self, *args):
>             print("exit")
>
>     foo = Foo()
>
>     def my_gen_f():
>         while True:
>             with foo:
>                 yield True
>
>     def f():
>         my_gen = my_gen_f()
>         next(my_gen)
>         print("after")
>
>     f()
>
>     print("finally")
>
> prints:
>
>     enter
>     after
>     exit
>     finally
>
> This is unrelated to the GC, disabling it does not change the output.
>

Looks like PEP-0533 mostly fixes the issue in python.
https://stackoverflow.com/questions/41881731/is-it-safe-to-combine-with-and-yield-in-python#comment70959811_41881820
suggests that the GC is still involved.


I'm wondering if `with` in lua could just have more methods than
:open() and :close().
e.g. :yield() and :resume() if a `with` block is yielded past.