lua-users home
lua-l archive

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


I have a question about working around the behavior of metatables when it comes to invoking the "__eq" or "==" abstraction for a userdata and `nil`.

From the docs (emphasis mine):

Lua will try a metamethod only when the values being compared are either both tables or both full userdata and they are not primitively equal. The result of the call is always converted to a boolean.

This prevents me from doing "some_userdata == nil" and having it return true in the case of working with something like a nullptr. I want to keep the ease-of-use/"C-like" style of comparing against Nil, but I cannot invoke a metamethod to check. Does anyone have any suggestions? I'm open to just about anything, and obviously the more elegant/syntax-clean suggestion the better.

Background:
Previously, I used to return an actual "nil" when I detected "nullptr" in the connected C code, but I can't do that anymore due to complicated typing issues that can be somewhat explained in these github issues:

https://github.com/ThePhD/sol2/issues/434#issue-239098943
https://github.com/ThePhD/sol2/issues/419#issue-234207101

I might revert the change back and tell the users to use a higher-level abstraction that's not std::shared_ptr/std::unique_ptr, or crash if I can't convert rather than returning a nullptr-containing version of these C++ abstractions, but.... well, it's hard figuring out which is the right way to go.