lua-users home
lua-l archive

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


Right now the default value of a table is nil. It is the one value that can never be *in* a table. Reading a non-existent key returns this value . Assigning this value to a key removes it from the table.

What if there was a formal way to set/get the internal "default value" of a table? For example, if I were to call setdefault(t, -1), then t could contain any value except -1. Reading a non-existent key would now return -1. Assigning -1 to a key would delete it.

It would be generally useful as a convenience. Plus if I were to set the default value of a table to some atom, I could make an "array" which could contain any other lua value, including nil.

Of course, you'd now have to test return values against a table's default value rather than nil

  function exists(t, key) return t[k] ~= getdefault(t) end

Calling setdefault() on a non-empty table would raise an error.

Its a half-baked idea. I haven't really thought through the implications. Is it just trading one problem for another? Could it be implemented efficiently? But I thought maybe it could lead somewhere.

Scott