[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: [hack] nil.foo = nil
- From: Sean Conner <sean@...>
- Date: Thu, 1 May 2014 21:56:56 -0400
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.