These are chat archives for ChaiScript/ChaiScript

9th
Jun 2016
Phil Cooper-King
@PhilCK
Jun 09 2016 11:59
Is there a way to check for the existence of a function in a chaiscript, without having to deal with exceptions?
auto func = chai.eval<std::function<void()>>("no_func_to_find");

if(func) {
   func();
}
Jason Turner
@lefticus
Jun 09 2016 14:33
at the moment some form of robust "does this method exist" check seems to be the most requested feature, so I'll try to get that in. In the meantime, the best you can do is call get_function_objects from the chaiscript side, so something like:
Jason Turner
@lefticus
Jun 09 2016 14:43

@PhilCK

 def function_exists(String name) { return contains(get_function_objects(), fun(elem){ return elem.first == name; }); }

Then in C++ you might do:

auto function_exists = chai.eval<std::function<bool(const std::string &)>>("function_exists");
if (function_exists("no_func_to_find")) {
  // function exists
} else {
  // it doesn't
}

No exceptions should be executed at all even internally during these calls.

@roig you might also find this discussion interesting

I hope that is helpful
Phil Cooper-King
@PhilCK
Jun 09 2016 14:58
Thanks @lefticus, that is helpful.
Daniel Guzman
@roig
Jun 09 2016 15:21
I will try this approach, thanks @lefticus