lua-users home
lua-l archive

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


On 2/12/2010 12:13 PM, spir wrote:
Hello,

I'm looking for a way to "subtype" Lua files, for file objects to
hold additional attributes. It seems I cannot directly set attributes
on Lua Files, since they are userdata. Is there a way to write a
custom file type that delegates file methods to an actual Lua file
(open in mode 'r')? My trials using __index with a table or function
launch errors like:


One way would be a global table for file attributes with the file handles as weak keys.

FileAttributes = setmetatable({}, {__mode="k"})
function setfileattr(F,K,V)
  local FAT = FileAttributes[F] or {}
  FAT[K] = V
  FileAttributes[F] = FAT -- in case a new table is created
end
function getfileattr(F,K)
  return (FileAttributes[F] or {})[K]
end

--
- tom
telliamed@whoopdedo.org