I often make extensions for embedded tables. I do this primarily when I think that something is really important missing. Capitalization has not made my “important” list, but something called string.split has, for example.
When I do this, I follow the programming convention:
require 'stringutil' -- load extra stuff into string.* require 'osutil' -- load extra stuff into os.*
You get the idea.
Another thing that I do when I try to be sure that I am not overwriting something that does not exist yet, so that I can be confident in the future:
function extend(tab, field, val) if tab[field] == nil then tab[field] = val return val elseif tab[field] ~= val then error(string.format('Extension %s.%s failed: already occupied by %s', nameof(tab), field, tostring(val))) else return val end end
The nameof function looks like this:
function nameof(val) for name, v in pairs(_G) do if v == val then return name end end return '?' end
Final note: when I intend to share code with others, I try not to modify the predefined tables. According to the Golden Rule, this namespace is shared by everyone, and if I have other people using my code, it’s not fair for me to just take all the fields that I want in a predefined string table.
Norman ramsey
source share