satoren / kaguya

C++ binding to Lua
Boost Software License 1.0
345 stars 70 forks source link

Returning nil in multiple returns #96

Closed codeaddicted closed 4 years ago

codeaddicted commented 4 years ago

Hi,

im trying to return nil in a list of multiple values from Lua to C++ and i can't get it to work.

Small Example:

kaguya::State state;
state(R"(multresfun =function() return "Hello ", nil, " World" end)");
std::string one, two, three;
kaguya::tie(one, two, three) = state["multresfun"]();
std::cout << one << two << three << std::endl;

it results in the following error when executed:

terminate called after throwing an instance of 'kaguya::LuaTypeMismatch'
  what():  type mismatch!!

I don't care if i need to loop over a result set and check a type() function instead of using kaguya::tie or a std::tuple i just need to be able to return nil-values or at least check if a specific return value in the set is nil

Is there any way to achieve returning a nil value in a multipe value return?

Best Regards Christoph

satoren commented 4 years ago

Hi

Please try kaguya::optional<std::string>

codeaddicted commented 4 years ago

Hi,

awesome that seems to work.

Documenting my solution here for others to be found:

kaguya::State state;
state(R"(multresfun =function() return "Hello ", nil, " World" end)");
std::string one, three;
kaguya::optional<std::string> two;
kaguya::tie(one, two, three) = state["multresfun"]();
if(!two) {
    std::cout << "two is empty!" << std::endl;
} else {
    std::cout << one << two.value() << three << std::endl;
}

Hoping i used it correct :)

Best Regards & a big THANK YOU Christoph