
ChaiScript – Easy to Use Scripting for C++ - rsp1984
http://chaiscript.com/
======
Erlich_Bachman
I have been looking through it, and examples, and code, and I guess the main
question condensing in my mind is one big "Why"? Why use this, why yet another
scripting language?

Why not just use Lua, one of JavaScript engines, Python, etc? They all have
C++ bindings, all relatively easy to use. All are already used in many C++
apps for this exact purpose... (I guess it's not commonly known in all coder
circles, but all those languages can just as easily be embedded in a C++
codebase as this.)

~~~
mehrdadn
JS and Python are slow [see below] and heavyweight. Lua might work, I don't
know... but this being header-only makes it pretty nice for simpler
situations.

EDIT: Sorry, I wrote this a little too quickly and ended up writing a bit too
coarse of a description. I meant JS and Python are both heavyweight to include
in a project, and Python is slow both to initialize and to run. I didn't mean
JS is slow to run. I don't know how fast it starts up either.

~~~
rienbdj
It is ridiculous that header-only is considered an advantage. The state of C++
build tools is very poor, and it is harming the language as a whole.

~~~
geezerjay
> It is ridiculous that header-only is considered an advantage.

Why do you believe it's ridiculous? Being able to integrate a third-party
library by just adding a few source files to your source tree is as simple as
it gets.

> The state of C++ build tools is very poor, and it is harming the language as
> a whole.

This assertion makes no sense, particularly in the light of this discussion.
Installing a headers-only library is a solved problem, and even template-heavy
libraries such as Eigen are already distributed and installed quite easily
with standard linux package managers.

~~~
rienbdj
I should clarify. Being able to add headers to a project in C++ is easy but
adding translation units is not (usually). This encourages header-only
libraries even when they are not really appropriate, increasing compilation
times etc.

~~~
_pmf_
Sometimes I wonder why projects are either "single header only" or tens to
hundreds of separate modules. One header and one source module would be a nice
compromise regarding compile time vs. ease of use.

Yes, I realize that some header only libraries support using the header as
either header or implementation, but this still blows up compile time.

~~~
BubRoss
Does it really blow up compile time? Translation units and optimization are
usually where compile time is spent. Having fewer but fatter translation units
helps compile times tremendously while most of the time per translation unit
is spent in stages beyond the source compiling in LLVM.

Boost is really the only example I can think of where small utility comes at
the expense of huge compile time increases.

------
colmvp
I just have to upvote this since Jason Turner is one of the creators and I owe
a ton of my knowledge about C++ to his videos on his YT channel, as well as
his talks at CppCon.

~~~
dgellow
He's also co-host of the podcast CppCast :)
[https://cppcast.com/](https://cppcast.com/)

------
szappetx
I really like ChaiScript and have used it in a couple of small game projects.

But I stopped using it because it was insanely slow (in both compile times and
runtime performance). Several hundred (if not thousand) times slower than the
equivalent Lua code.

That was a few years ago, but skimming through the release notes, it doesn't
seem to have been fixed.

~~~
kevin_thibedeau
I bet the compile times would improve if it wasn't header only.

~~~
szappetx
Unfortunately ChaiScript needs to be header-only because it's like 99%
templates.

You can put all your ChaiScript-related code into an isolated implementation
file, and try to avoid touching that file as much as possible. But that only
works well for long-term projects, where once you get all the bindings you
need set up, you can coast for a while without touching those files.

For short-term projects it's a nightmare because you're constantly touching
the ChaiScript files.

There's no inherent reason a header-only library should significantly impact
compile times, aside from the fact that the authors usually don't have the
foresight to make it efficient. It's also very difficult to find information
about how to make templates more efficient at compile time.

------
adev_
For the note: ChaiScript is from Jason Turner. He also does CPPCast (
[https://www.youtube.com/channel/UCuCjADS4u3uJDTqUaG0H9dA](https://www.youtube.com/channel/UCuCjADS4u3uJDTqUaG0H9dA))
and is a regular presenter at CPPCon (
[https://www.youtube.com/watch?v=zBkNBP00wJE](https://www.youtube.com/watch?v=zBkNBP00wJE)
).

Many of his videos / tutorials are worth a look if you are interested in C++.

------
Bootwizard
Wow this looks awesome, great job! I didn't even know I wanted this until I
saw it.

I am worried though how memory management is handled under the hood. Is it
possible to use traditional C++ memory management with any of the variables
created in ChaiScript?

I'm not saying that I don't trust the implementation, I'd just like more
control over it in some cases.

------
azhenley
Looks very nice! It would be helpful to have a page that compares it to some
of the other popular scripting languages for C++.

------
lxe
I like this and I see the use cases, however why would I use ChaiScript over
Lua for example?

~~~
rsp1984
To me it looks like ChaiScript is more lightweight and easier to learn.

~~~
tcallred
Would that make it a good candidate for microcontroller programming? I have
been looking for something more elegant/functional to program arduino with.
Anything about memory size on the site?

~~~
tyingq
_" ChaiScript is a header only library with only one dependency: The operating
system provided dynamic library loader, which has to be specified on some
platforms."_

I believe the dynamic library loader requirement is a showstopper for AVR.

------
mhh__
> ChaiScript has been making stable releases since 2009. No bugfix or feature
> is made without a corresponding unit test

Codecov: 72%

Reality is often disappointing...

In all seriousness, looks pretty nice.

~~~
bbmario
Since when code coverage is accurate enough for you to make that judgement?

~~~
mhh__
Clearly I made the mistake of trying to tell a joke in this sacred forum.

72% is more than high enough for me to believe them, but it's not 100% hence
the gag

~~~
SCLeo
...Yeah, your joke isn't really successful... I didn't realize it is a joke
either.

------
vishnugupta
Ha! Chai == Tea in Hindi, for those who are wondering ;)

~~~
mlevental
chai is tea in many languages that either inherit from Russian or Arabic

[https://www.etymonline.com/word/chai](https://www.etymonline.com/word/chai)

Edit: apparently the original source is China. Etymonline fail!

~~~
vezycash
With few exceptions, the word for Tea in a country depends on how it was
originally transported there.

See: Tea if by sea, cha if by land: Why the world only has two words for tea

[https://qz.com/1176962/map-how-the-word-tea-spread-over-
land...](https://qz.com/1176962/map-how-the-word-tea-spread-over-land-and-sea-
to-conquer-the-world/)

~~~
vishnugupta
Wow. I had absolutely no idea! Thanks for sharing that link, learned something
new today

------
s_y_n_t_a_x
LuaJIT w/ FFI seems better imo.

------
jokoon
I just discovered that raw multi line string don't work on visual studio
2017...

~~~
pooya13
With the paratheses? R”(str)”

~~~
jokoon
Oh right braces are needed! Thanks!

------
Kiro
I'm stupid but what's the purpose? Why not just call the function directly
without Chai?

~~~
furyofantares
Calling a C++ function from your scripting language, or a scripted function
from C++ are different uses, they're just combined into one for the purposes
of a demo of what the code looks like.

edit: For example, there's a fairly clear distinction in a game between engine
code and gameplay code. Engine code is run all the time, and should be memory
and computationally efficient, and robust. It won't likely need to be changed
much, and is OK to spend some extra effort writing it to get all these
properties. So C++ is great for this.

Gameplay code doesn't tend to have these requirements, and you may not want to
pay the cost to write C++ if you don't need the benefits. And additionally,
gameplay code often needs to be rewritten frequently while you experiment with
gameplay to find what's fun, or do expansions, or support mods to the game, so
a language that's very easy to write in helps a lot.

