These are chat archives for ChaiScript/ChaiScript

8th
Dec 2016
M00NLight
@Puti_ZL_twitter
Dec 08 2016 03:43
@StanEpp Thank you! But I think functions with variadic params should be an important feature, like printf.
StanEpp
@StanEpp
Dec 08 2016 10:57
@Puti_ZL_twitter When I am not mistaken I don't think that this is even possible in chaiscript. But I am not the expert here :D
lucas-sarie
@lucas-sarie
Dec 08 2016 12:41
@Puti_ZL_twitter You can use chaiscript array definition to workaround this issue. I have done a printf implementation by this way, I can share code if you are interested, syntax is like : ("this is a test %s, number : %d" % ["arg1", 1])
or : "this is a test %s, number : %d".Format(["arg1", 1])
Jason Turner
@lefticus
Dec 08 2016 15:45
Registering a variadic function with ChaiScript is impossible - C++ cannot allow such a thing
but it would be theoretically possible to do something like initializer_list support
DragonLordK
@DragonLordK
Dec 08 2016 19:26
Hello
Hello
Jason Turner
@lefticus
Dec 08 2016 19:37
Hello Hello
StanEpp
@StanEpp
Dec 08 2016 19:54

@lefticus I think I am a little bit stuck with reference collapsing right know. Having

template<class... Args>
void func1(const Args&&... params) {
  std::cout << "Hello World!\n";
}

void func2(const int& x, const int& y) {
  std::cout << "Hello " << x << " " << y << " !\n";
}

Why does the first evaluation fail but the second one succeeds?

chaiscript::ChaiScript chai;
chai.add(chaiscript::fun(&func1<int, int>), "func1");
chai.add(chaiscript::fun(&func2), "func2");
chai.eval("func1(10, 23)");
chai.eval("func2(10, 23)");
StanEpp
@StanEpp
Dec 08 2016 20:13
EDIT: It should be
... void func1(Args&&... params) ...
... chai.add(chaiscript::fun(&func1<const int, const int>), "func1") ...
Jason Turner
@lefticus
Dec 08 2016 20:28
fail at runtime or fail at compile time? @StanEpp
StanEpp
@StanEpp
Dec 08 2016 20:28
Runtime

It basically boils down to this function signature which generates an dispatch error at runtime when used:

void func2(int&& x) {
  std::cout << "Hello " << x << " !\n";
}
chai.add(chaiscript::fun(&func2), "func2");
chai.eval("func2(1)");

It generates: Error with function dispatch with function 'func2'" With parameters: (const int)

Jason Turner
@lefticus
Dec 08 2016 20:32
That's what I was thinking
I'm going to argue that you really don't want your function to be void func2(int &&)
and right now, it's true that chaiscript is only going to be willing to pass a non-const value to a && parameter
while C++ would allow that
StanEpp
@StanEpp
Dec 08 2016 20:36
I am a bit lost. func2(1) in c++ does compile and executes correctly, but how is the argument passed to func2?
Jason Turner
@lefticus
Dec 08 2016 20:39
in C++ it's going to make a temporary copy under the covers which then becomes an r-value that it can pass to the int && parameter
@StanEpp take this example: The unique_ptr has to be copied to be passed to the && parameter, since it is const but unique_ptr is not copyable, so the build fails
StanEpp
@StanEpp
Dec 08 2016 20:42
Ahh, so this "under the covers" magic is preventing chaiscript from dispatching the function in the same way as in plain c++ code?
Jason Turner
@lefticus
Dec 08 2016 20:42
Actually, let me double check something
sorry that was inaccurate
in C++ (1) is an r-value, which is why it can be passed to the &&, but there's really no way for me to do that with the way I handle temporary values
I don't know what you are actually using your funcs for, but would it make more sense to do (const Arg & ... ) and do const-references instead?
Jason Turner
@lefticus
Dec 08 2016 20:49
You're really right on the edge of what ChaiScript is capable of doing here.
StanEpp
@StanEpp
Dec 08 2016 20:51

I am writing a factory class where I can register particular classes at. The factory class provides the chaiscript modules with all relevant functions. So what I am basically doing is this:

template<class TComp, class... TArgs>
void addComponent(TArgs&&... params) {
   // do other stuff
}

template<class TComp, class... TArgs>
void registerComponent(const std::string& name) {
  // ......
  m_chaiDefModule->add(chaiscript::fun(&Factory::addComponent<TComp, TArgs...>),  name);
  // ......
}

That's where I stumbled upon this problem. I need to rethink this carefully and adjust everything accordingly.

But thanks for the help! Things got clearer for me. =)
Jason Turner
@lefticus
Dec 08 2016 20:57
for what it's worth, you might be able to do: func2(int(1)) to explicitly make a copy that could be converted into in rvalue in ChaiScript
it's also possible that I could add in a "make a new copy" kind of feature for C++ types that are trivially copyable
StanEpp
@StanEpp
Dec 08 2016 21:00
Yes this works! =) But I think I don't want to enforce the user to write it like that. I'll just simply rework my methods.
Jason Turner
@lefticus
Dec 08 2016 21:01
OK. So there might still be an internal solution, but I don't know how complete I could make it
it would have to be something like "if the type is copyable, and const, then make a copy of it when passing to a && function"
StanEpp
@StanEpp
Dec 08 2016 21:03
This might already work for me :D
Jason Turner
@lefticus
Dec 08 2016 21:04
ok