lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great Andrew Starks once stated:
> Here is a hack that I like a lot:
> 
> ```lua
> 
> debug.setmetatable(nil, {
>     __index = function(t, i)
>         return nil
>     end
> })
> 
> local this = {}
> 
> if this.field.does.NOT.exist == nil then
>     print('no error!')
> end
> 
> --> no error!
> ```

  Wow.

  I'm of two minds on this.  On the one hand, I love the idea, because there
are times when I want this behavior.  On the other hand, this can hide some
serious bugs in a code base.  On the gripping hand [1], I wouldn't know how
to deal with a codebase that does this.

  -spc (I never thought that nil could have a metatable associated with it)

[1]	The two hardest things in computer science: naming, cache
	invalidation and off-by-one errors.