Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Example self-implementation
- local old_type = type
- function type(object)
- if old_type(object) ~= "table" then
- return old_type(object)
- else
- local mt = getmetatable(object)
- if old_type(mt) == "table" then
- if old_type(mt.__type) == "function" then
- return mt.__type(object)
- else
- return mt.__type
- end
- else
- return "table"
- end
- end
- end
- -- This will work
- local ok_table = {
- __type = "OK table"
- }
- setmetatable(ok_table, ok_table)
- print(type(ok_table))
- -- > OK table
- -- This will *not* work; setting __metatable blocks our new
- -- type() function from working
- local bad_table = {
- __type = "Bad table"
- __metatable = "No can has"
- }
- setmetatable(bad_table, bad_table)
- print(type(bad_table))
- -- > table (in this case)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement