[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: metatables and == nil
- From: Duane Leslie <parakleta@...>
- Date: Fri, 30 Jun 2017 12:30:30 +1000
> On 30 Jun 2017, at 10:01, Eric Man <meric.au@gmail.com> wrote:
>
> What about writing your own null checking function?
If the target is just truthy/false you could overload the binary not operator. To get consistent behaviour with `nil` you'd also have to modify its metatable as follows:
```
debug.setmetatable(nil,(debug.getmetatable(nil) or {}).__bnot = function () return true end)
```
Then you'd be able to say:
```
if ~userdata then
print("null/nil userdata")
else
print("valid userdata")
end
```
And it would work for both your userdata type and the nil type.
Regards,
Duane.