You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I just noticed (the hard way) that util.warn only warns against reading unexisting variables, not unexisting table members:
require'util.warn'-- generates an error-- local x = ylocalt= {}
-- does not generate an errorlocalx=t.y
This is very problematic when using object-style, since self is always a table: we can thus read from inexisting fields. Would it be possible to warn against this, the same way you check for unexisting members in _G?
The text was updated successfully, but these errors were encountered:
I'm posting the code for the simplest checking here.
If you want to check torch classes and nested tables,
some extra logic would need to be integrated.
You can experiment with the metatable methods __index and __newindex.
local function guardReads(table)
setmetatable(table, {
__index = function (table, key)
error("missing key: "..tostring(key), 2)
end,
})
end
local t = {}
guardReads(t)
-- will generate an error
local x = t.y
I worry that calling guardReads(self) would not work correctly.
The torch class has already an existing metatable. The metatable provides a fallback to read attributes from the parent.
You can start using the above guardReads() in your code and discover the problems.
In other words: pull-requests are welcome.
Hi Jeff, Andreas @akfidjeland
I just noticed (the hard way) that util.warn only warns against reading unexisting variables, not unexisting table members:
This is very problematic when using object-style, since
self
is always a table: we can thus read from inexisting fields. Would it be possible to warn against this, the same way you check for unexisting members in_G
?The text was updated successfully, but these errors were encountered: