lua-users home
lua-l archive

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


>>>>> "Mimmo" == Mimmo Mane <pocomane_7a@pocomane.com> writes:

 Mimmo> Normally, you can declosify just overriding the __close()
 Mimmo> metamethod, but here it is impossible without affecting the
 Mimmo> other files. You can still use the proxy trick suggested by
 Mimmo> Andrew

Incidentally, I forgot to mention the main reason for the proxy, which
is to be able to _return_ the value.

e.g.

function f1(fn)
  local fc <close>, f, err, errno = auto_close(io.open(fn,"r"))
  --[[ check for errors ]]
  --[[ do stuff with f, that might throw an error ]]
  -- if successful, return the file to the caller
  return fc:release()
end

function f2()
  local fc <close>, f = auto_close(f1("filename"))
  --[[ do stuff ]]
end

This does leave a small window for error in the auto_close call itself,
but I believe with the C implementation and with no intervention from
hooks, the only likely failure is an "out of memory" on allocating the
new proxy object.

-- 
Andrew.