Can you please make metatables output optionally disablable?
lua-rocks opened this issue · 5 comments
from factorio stdlib
local function no_meta(item, path)
if path[#path] == inspect.METATABLE then
return {item.__class}
end
return item
end
--- Inspect the class
function Core.inspect(self)
return inspect(self, {process = no_meta})
end
@vyacheslav-nosov thank you for the answer but this solution removes __tostring
comments from output
I don't think this will be ever implemented on this library.
May I suggest using this instead:
local inspect = require "inspect"
local function no_meta_with_description(item, path)
if path[#path] == inspect.METATABLE then
return { __tostring = item.__tostring }
end
return item
end
local function my_inspect(t)
return inspect(t, {process = no_meta_with_description})
end
--- tests:
local person_mt = {
__tostring = function(t) return "A person named " .. t.name end
}
-- add 100 fields to person_mt
for i=1,100 do
person_mt["field" .. tostring(i)] = true
end
local peter = setmetatable({ name = "peter" }, person_mt)
local john = setmetatable({ name = "john" }, person_mt)
print(my_inspect(peter))
print(my_inspect(john))
The output will be similar to what you want. Tables will have their __tostring
comments. They will still have a metatable field, but it will be very small, with only one field.
{ -- A person named peter
name = "peter",
<metatable> = {
__tostring = <function 1>
}
}
{ -- A person named john
name = "john",
<metatable> = {
__tostring = <function 1>
}
}
I hope this is an acceptable compromise.
@kikito for all of these "wontfix" it will avoid a lot of confusion if the workarounds you mention, are documented in https://github.com/kikito/inspect.lua#examples-of-use . Or at least, mark these issues with a tag like wontfix
or workaround
and link to that issue search from the readme.