[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: File-names vs file-descriptors and question on io library way of reporting errors.
- From: KR <krunal.rao78@...>
- Date: Thu, 14 Jul 2011 18:13:23 +0000 (UTC)
Hi,
Mostly for data containers (vectors, matrices of numbers) the typical code for
writing data to files is:
local f = assert(io.open("somefile.txt", "w"))
-- follows asserted code where I write the elements of the container to f
f:close()
This is all quite repetitive so I ended up with something like (I really have
this as a "memeber function"):
local function write(array, filename, mode)
mode = mode or "w"
local f = assert(io.open(filename, mode))
-- follows asserted code where I write the elements of the container to f
f:close()
end
What I am wondering about is mainly whether it's a good practice to pass around
filenames instead of filedescriptors (handles).
I was also interested in the motivations that led to prefer for the io library
the approach of "non-signalling errors" (i.e. nil + error message on error)
instead of the "signalling" one (error(errormessage) on error).
I am finding myself surrounding (almost) all the io operations with asserts,
while with the signalling approach this would not be necessary and if action can
be taken to recover from the error this action can be implemented via pcall.
It's not a critique, I am just curious =)
Thanks for any reply (on these not-so-interesting) questions.