mirror of
https://github.com/ChaiScript/ChaiScript.git
synced 2025-12-06 16:57:04 +08:00
Added namespace documentation to the cheatsheet.md
This commit is contained in:
parent
d61e322c1d
commit
c45be80bf5
@ -119,6 +119,18 @@ chai.add_global(chaiscript::var(somevar), "somevar"); // global non-const, throw
|
|||||||
chai.set_global(chaiscript::var(somevar), "somevar"); // global non-const, overwrites existing object
|
chai.set_global(chaiscript::var(somevar), "somevar"); // global non-const, overwrites existing object
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Adding Namespaces
|
||||||
|
|
||||||
|
```
|
||||||
|
chaiscript::Namespace math;
|
||||||
|
math["pi"] = chaiscript::const_var(3.14159);
|
||||||
|
math["sin"] = chaiscript::var(chaiscript::fun([](const double x) { return sin(x); }));
|
||||||
|
chai.register_namespace(math, "math");
|
||||||
|
```
|
||||||
|
|
||||||
|
Namespaces are imported to make them available for scripting
|
||||||
|
```
|
||||||
|
|
||||||
# Using STL
|
# Using STL
|
||||||
ChaiScript recognize many types from STL, but you have to add specific instantiation yourself.
|
ChaiScript recognize many types from STL, but you have to add specific instantiation yourself.
|
||||||
|
|
||||||
@ -423,6 +435,20 @@ o.f = fun(y) { print(this.x + y); }
|
|||||||
o.f(10); // prints 13
|
o.f(10); // prints 13
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Namespaces
|
||||||
|
|
||||||
|
Namespaces in ChaiScript are Dynamic Objects with global scope
|
||||||
|
|
||||||
|
```
|
||||||
|
namespace("math") // create a new namespace
|
||||||
|
|
||||||
|
math.square = fun(x) { x * x } // add a function to the "math" namespace
|
||||||
|
math.sum_squares = fun(x, y) { math.square(x) + math.square(y) }
|
||||||
|
|
||||||
|
print(math.square(4)) // prints 16
|
||||||
|
print(math.sum_squares(2, 5)) // prints 29
|
||||||
|
```
|
||||||
|
|
||||||
### Option Explicit
|
### Option Explicit
|
||||||
|
|
||||||
If you want to disable dynamic parameter definitions, you can `set_explicit`.
|
If you want to disable dynamic parameter definitions, you can `set_explicit`.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user