About ChaiScript

ChaiScript is the first and only scripting language designed from the ground up with C++ compatibility and modern design in mind. It is an ECMAScript-inspired, embedded functional-like language.

ChaiScript is licensed under the BSD license.

Download

Due to an increased release schedule, and the nature of ChaiScript being a header-only library, binary releases will be made less often. Previous releases can be found on github.

Version: 4.2.0 Released: 12/1/2012
(Requires Boost)
Source (tar.bz2)
Source (zip)

Version: 5.2.0 Released: 12/1/2012
(Requires C++11 Compiler)
Source (tar.bz2)
Source (zip)

Function with optional parameter

How bind a function with optional parameterparameter with Chaiscript ?

I try this :

I try with overloaded function :

    boost::function<void(std::string&,std::string,int,int,int,int,std::string)> f = boost::bind(&ModGUI::createContainer,this,_1,_2,_3,_4,_5,_6,_7);
    chai->add(fun(f), "createContainer");
    boost::function<void(std::string&,std::string,int,int,int,int)> f2 = boost::bind(&ModGUI::createContainer,this,_1,_2,_3,_4,_5,_6,"mapium");
    chai->add(fun(f2), "createContainer");
    chai->eval("createContainer(\"teste\",\"none\",10,10,100,100);"); // crash and no execption
    boost::function<void(std::string&,std::string,int,int,int,int,std::string)> f = boost::bind(&ModGUI::createContainer,this,_1,_2,_3,_4,_5,_6,_7);
    chai->add(fun(f), "createContainer");
    chai->eval("createContainer(\"teste\",\"none\",10,10,100,100,\"mapium\");"); // crash and no execption

I also want to ask how to do

I also want to ask how to do this correctly. Right now I can only write a wrapper function for each default argument, which is very tedious.

I'm sorry, but there's no way

I'm sorry, but there's no way to get the C++ compiler to tell us that there are default arguments for a function. Your options are to wrap the function in ChaiScript to provide the extra versions or wrap them in C++. If you wrap it in C++ you get the advantage of maintaining the default arguments as provided.

void myfunc(int i=1, int j=2, int k=3, int l=4);
 
void myfuncwrap() { myfunc(); }
void myfuncwrap(int i) { myfunc(i); }
void myfuncwrap(int i, int j) { myfunc(i, j); }
void myfuncwrap(int i, int j, int k) { myfunc(i, j, k); }
void myfuncwrap(int i, int j, int k, int l) { myfunc(i,j,k,l); }
 
 
chai.add(fun<void (int, int, int, int)>(&myfuncwrap), "myfunc");
chai.add(fun<void (int, int, int)>(&myfuncwrap), "myfunc");
chai.add(fun<void (int, int)>(&myfuncwrap), "myfunc");
chai.add(fun<void (int)>(&myfuncwrap), "myfunc");
chai.add(fun<void ()>(&myfuncwrap), "myfunc");

It's not pretty, unfortunately.

-Jason

Support ChaiScript by sharing it with your friends: