These are chat archives for ChaiScript/ChaiScript

21st
Feb 2015
Tomáš Mládek
@tmladek
Feb 21 2015 16:15
is it possible to somehow completely override a function (specifically, print()) with my own C++ version? If I simply register it and name it print, it works when called directly, but not when used as a parameter in say, for_each
Jason Turner
@lefticus
Feb 21 2015 18:33
I'd need more details. Passing it to for_each should be no different than calling it directly - the same overload resolution takes place in either case
Tomáš Mládek
@tmladek
Feb 21 2015 18:37
interesting; well this should be the relevant portions: https://gist.github.com/tmladek/c01ff6ffc6e5a45ce022
Jason Turner
@lefticus
Feb 21 2015 18:54
so what does the code look like that is failing?
The chaiscript side
Tomáš Mládek
@tmladek
Feb 21 2015 18:55
oh, right
print("This prints as expected by the override");
for_each(look(), print);
where look() is also a method of Class, returning a std::vector<chaiscript::Boxed_Value>
and it returns a bunch of (custom defined) objects which have the to_string func defined
Jason Turner
@lefticus
Feb 21 2015 19:01
so that's why it's failing. Your print function will be called only if it's called on a string value specifically. By passing a generic set of objects to it, the built in print(Boxed_Value) overload will be called
to get the behavior you want, you need to either not name it print, or do something like this:
Tomáš Mládek
@tmladek
Feb 21 2015 19:02
Oh, I feel a bit stupid now
Jason Turner
@lefticus
Feb 21 2015 19:02
for_each(look(), fun(x) { print(x.to_string()); })
so you know you are calling your std::string overload
Tomáš Mládek
@tmladek
Feb 21 2015 19:03
right, thank you!
Tomáš Mládek
@tmladek
Feb 21 2015 19:21
Is it possible to invoke the to_string from within C++, though? I'd prefer if I had control over all chaiscript-printing
or I guess I could just redefine print() from within chaiscript
Tomáš Mládek
@tmladek
Feb 21 2015 19:36
right, that works fine, sorry for the nuisance!
Jason Turner
@lefticus
Feb 21 2015 19:56
just as an fyi, you could do something like: auto to_string = chai.eval<std::function<std::to_string (const Boxed_Value &)>>("to_string");
std::string str = to_string(var(some_object))
that should (it's untested) provide you with a C++ function object that dispatches to the appropriate chaiscript to_string function based on the type of the object you pass in
if you wanted to avoid copying the object:
to_string(var(std::ref(some_object)))
Tomáš Mládek
@tmladek
Feb 21 2015 19:58
clever, I hadn't thought of that
Jason Turner
@lefticus
Feb 21 2015 19:58
the caveat being that it will only wrap to_string definitions that exist at the moment you create the to_string std::function object
if you add a new chaiscript defined to_string, it will not be called by your old method
you could handle that case too, by defining a chaiscript function that calls to_string
then exposing that chaiscript function to C++
thusly: auto to_string = chai.eval<std::function<std::to_string (const Boxed_Value &)>>("fun(x) { return x.to_string();}");
that's the generic, expandable solution
Tomáš Mládek
@tmladek
Feb 21 2015 20:04
thank you kindly!
Jason Turner
@lefticus
Feb 21 2015 20:09
If I might ask, what kind of project are you working on?
Is it something that's open source?
Tomáš Mládek
@tmladek
Feb 21 2015 20:09
It's a school project, a game that you don't play directly, but instead program rudimentary AIs which then "play" the game
it is eventually going to be open-source
Jason Turner
@lefticus
Feb 21 2015 20:14
I was recently thinking chaiscipt would make a good match for a game like that, actually. I had a teacher back in 1994 tell me about some game where you fought programs against each other.
good luck, let me know if you have any other questions
Tomáš Mládek
@tmladek
Feb 21 2015 21:40
oh, and the "Getting started with scripting" links on http://chaiscript.com/docs.html are dead
Jason Turner
@lefticus
Feb 21 2015 22:07
oh wow, thanks for pointing that out
Tomáš Mládek
@tmladek
Feb 21 2015 23:37
I just ran into an issue with getting/setting states, the equivalent of this: https://gist.github.com/tmladek/df5293c23594acb51208 makes ChaiScript throw an Error: "Variable redefined 'a'" when exec() is called multiple times