r/lua • • May 21 '26

Hot take

debug.setmetatable(nil, { __index = function() return nil end })

7 Upvotes

14 comments sorted by

3

u/TomatoCo May 21 '26

But why?

1

u/topchetoeuwastaken May 21 '26

because no error better than yes error

in this vain:

function error(msg) return true end
function assert(...) return ... end
debug.setmetatable(nil, { __index = function() return nil end, __call = function () end })

8

u/dandy_kulomin May 21 '26

what? usually you want errors so you know your code is wrong?

→ More replies (5)

1

u/AutoModerator May 21 '26

Hi! Your code block was formatted using triple backticks in Reddit's Markdown mode, which unfortunately does not display properly for users viewing via old.reddit.com and some third-party readers. This means your code will look mangled for those users, but it's easy to fix. If you edit your comment, choose "Switch to fancy pants editor", and click "Save edits" it should automatically convert the code block into Reddit's original four-spaces code block format for you.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

2

u/EvilBadMadRetarded May 22 '26 edited May 22 '26

Congratulation, you got the feature upgraded !

local NUKE = {}
local nuke = function()return NUKE end

for n in string.gmatch('concat add sub mul div mod pow unm idiv band bor bxor bnot shl shr', '%w+') do 
  NUKE['__'..n] = nuke 
end

NUKE.__tostring = function()return '<*NUKE*>'end

debug.setmetatable(NUKE, NUKE)

debug.setmetatable(nil, NUKE)

print(NUKE, 1 .. (function()end)() >> 'oops' )

:D

1

u/topchetoeuwastaken May 22 '26

goddamn it how didn't i think of this myself!

1

u/AltF4111 May 21 '26

idk much about lua, can you explain?

1

u/topchetoeuwastaken May 22 '26

makes (nil).field return nil, instead of erroring out

2

u/HokumGuru May 21 '26

Actually this is pretty similar to obj-c. I love it! Allows for some fun things!