C++Script is a dynamic programming language which compiles using a standard C++ compiler. C++Script can be used without an in-depth knowledge of C++.
The main benefit of using C++Script over other scripting languages is its seamless integration with C and C++. This provides integration into existing projects, and allows an easy "escape to C or C++" for performance reasons or for interfacing with particular libraries. The language is compiled so programs can be run without distributing an interpreter or a heavy runtime.
From a C++ perspective, scripting and dynamic programming have a lot to offer. Coding is often much easier, without the overhead of types and object lifetime. Scripting concepts like garbage collection and closures make complex patterns easier to write. The foreach macro makes iterating containers much easier.
C++Script consists of a C++ library, C++ header files, and an optional front-end to build and run programs. The front-end gives C++Script a more immediate feel, though in larger projects it would be more appropriate to use build tools such as make.
In a statically-typed language (like traditional C++), all variables have a type (such as char*) which is known at compile-time. Programmers must specify the type of every variable. In a dynamically-typed language, programmers do not specify any types, which is much simpler. The drawback with dynamic typing is that the types must be deduced as the programming is running, which slows the program.
C++Script provides a var type (which stands for variable, or variant). var is a static type containing a dynamic value (C# 4.0 uses the keyword dynamic for this concept). var can contain integers, strings, doubles, bools, objects, containers, functions, closures, C++ values etc. The type of the var is only known at run time, so var is dynamically typed (as opposed to other C++ types which are statically typed). var supports a full set of operators (via operator overloading), allowing vars to be used with a natural syntax in your code.
Programmers can either use var for dynamic typing, C++ types for static typing, or a mixture of the two.
A problem with dynamic typing is that type errors are only detected at run-time. Static types guarantee the correctness (at least regarding type safety), and provide early feedback to the programmer that something's wrong. With dynamic types, type errors could lurk for years before being discovered.
However if the only thing checking your code is static typing, then your code isn't tested properly. I wouldn't have much faith in code which compiles but hasn't been exercised. You need to have proper code coverage in unit tests, which will also catch all your type errors.
Types help define a logical structure to your program, and help to document it. However types also make programs rigid and resistant to change. The current dogma is that types are on balance a good thing, especially for large projects.
The static vs dynamic debate will continue, though I am personally optimistic that type inference will do a much better job of checking and optimising dynamic languages in future.
This manual is designed for people familiar with scripting
but not necessarily with C++. C++ veterans can skim this manual and just
read the examples and reference section.
Function names appear in italic, with a pair of brackets after them.
Sometimes the arguments are written, other times not. e.g. writeln().
Inline code examples are in italic. e.g. writeln("Hello world");
Code examples are in fixed-width font. Most examples are self-contained
so they can be run (much more useful than one-liners), and where possible a
link to a cpp file is given e.g. (hello_world.cpp)
#include <cppscript>
var script_main(var args)
{
writeln("Hello world!");
return 0;
}
String values are always quoted in itallic.
Method names and object members appear in quotes. e.g. Call the "writeln"
method to write a line to the file.
Where methods take arguments, they are written in brackets after the method
name. E.g. "writeln"(var line)
Exceptions are referenced by name, and are therefore quoted. e.g. var "readln"() throws "io_error"
if the file cannot be read due to an error.