Skip to content

Commit

Permalink
refactor(tablefunctions): make copy and invert functions, not methods (
Browse files Browse the repository at this point in the history
…#2726)

These methods can never actually be used as methods, because tables do
not automatically share a metatable like classes do. Thus `tbl:copy()`
does not know to look in `table` to find `copy()`.

This commit doesn't actually change any functionality, it just makes it
clearer how to use the functions.
  • Loading branch information
salinecitrine authored Mar 19, 2024
1 parent 957817c commit 670a86c
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions common/tablefunctions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ run for end users.)
]]

if not table.copy then
function table:copy()
function table.copy(tbl)
local copy = {}
for key, value in pairs(self) do
for key, value in pairs(tbl) do
if type(value) == "table" then
copy[key] = table.copy(value)
else
Expand Down Expand Up @@ -99,9 +99,9 @@ if not table.toString then
end

if not table.invert then
function table:invert()
function table.invert(tbl)
local inverted = {}
for key, value in pairs(self) do
for key, value in pairs(tbl) do
inverted[value] = key
end
return inverted
Expand Down

0 comments on commit 670a86c

Please sign in to comment.