Return more than one value in ChaiScript?

195 views Asked by At

In Lua it's possible to return more than one value, For example:

function math.pos(x1, y1, x2, y2)
  return x2 - x1, y2 - y1
end

distanceX, distanceY = math.pos(100, 100, 300, 300) --> distanceX = 0, distanceY = 0

Can I do something like this in ChaiScript?

1

There are 1 answers

1
veridis_quo_t On BEST ANSWER

This isn't possible in ChaiScript, although you can fake it using a structure containing multiple values.

You would have a structure (in C++) that would look a little like this:

struct multiple_return_value {
    int first;
    int second;
    multiple_return_value(int a, int b) : first(a), second(b) {}
};

You then register it with ChaiScript like so:

chai.add(chaiscript::user_type<multiple_return_value>(), "multiple_return_value");
chai.add(chaiscript::constructor<multiple_return_value(int, int)>(), "multiple_return_value");
chai.add(chaiscript::fun(&multiple_return_value::first), "first");
chai.add(chaiscript::fun(&multiple_return_value::second), "second");

Then you can use it like this:

def math_pos(x1, x1, y2, y2) {
    return multiple_return_value(x2 - x1, y2 - y1);
}

var distance = math_pos(100, 100, 300, 300);

print("distance x: " + to_string(distance.first));
print("distance y: " + to_string(distance.second));