lua-users home
lua-l archive

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


> 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.