Rapptz / sol

A C++11 Lua wrapper

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`sol::table`: Checking for existance of key

Nava2 opened this issue · comments

Is there a means for checking for an existence of a key within a sol::table?

Additionally, when using sol::table::operator[], it returns a sol::proxy. However, this type has no means of checking the type of the underlying value.

Something like this might be neat:

sol::table tbl; // assume it's initialized
if (auto prox = tbl["my_key"])
{
  // use prox
}
else
{
  // prox doesn't actually exist
}

A method such as exists() would be useful, too. I took this syntax suggestion from boost's optional type.

sol::object x = tbl["my_key"];
if ( x != sol::nil ) {
// good to go
}
else {
// not good to go
}

I don't think we have an explicit exists function, though. It would be nice to add, you are right. I'll keep it in mind as I keep developing.

That's great! I knew I must be missing something. You already have an issue open for documentation, I guess this is a great case and point for it.

For exists(), it would be nice. Frankly, inlining that check seems like a pretty quick and dirty solution.

Correction, objects.hpp:

bool valid() const {
        return !this->is<nil_t>();
    }

I'll be sure to add it to the documentation. Right now I'm finishing the featureset necessary to get sol v2.0 off the ground (work is being done here).

Closing this as I've found the use cases. 👍