lua-users home
lua-l archive

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


On Fri, Jul 31, 2020 at 8:19 PM Mimmo Mane wrote:
local function cowrap(f)
  local thread = coroutine.create(f)
  local closed = false
  local function doclose()
    if not closed then
      coroutine.close(thread)
      closed = true
    end
  end
  return setmetatable({},{
    __call = function(_,...)
      return coroutine.resume(thread,...)
    end,
    __close=doclose,
    __gc=doclose,
  })
end

Yes, this would work.
BTW, coroutine.close() is already idempotent, so no need for "if not closed then".