Value Types
CSML is a dynamically-typed language, even though it is written in Rust, a strong-typed, memory-safe, low-level language. It can handle a lot of different types but some magic happens behind the scenes to make it as easy for the developers to use the language.
In this section, let's learn more about how CSML handles the various quirks a chatbot needs to solve!
1
do number = 3
2
do string = "something"
3
do float = -4.7
4
do object = { "hey": "you" }
5
do array = [1, 2, number]
6
do boolean = true
Copied!

Literals

CSML is able to natively understand literal types (int, float, string, ...).
integer and float are separate types, but most of the time you should not have to worry about it and consider it as a more generic and virtual number type.
You can also express booleans with true or false .
Since CSML v1.1, you can also use \n, \t, \r , \ and " characters in strings, with proper escaping (\ and " must be preceded by a \ while a single \ will be ignored). For example:
1
say "Hi!\n\nWe are proud to \ present this nice \"CSML v1.1\" update!"
Copied!

NULL

NULL is its own type. Missing values evaluate to NULL. The CSML interpreter will automatically parse the object with the usual dot notation x.y.z and detect the type of the resulting property. If one of the properties in the chain does not exist or is not an object itself, it will evaluate to NULL.

Objects

You can create an object by returning a JSON-like object from any function, or directly in the CSML code with the Object() helper function or by using a shorthand notation similar to JSON format.
1
// Object representation
2
do obj1 = Object(
3
title = "toto",
4
body = Object(
5
somekey = "somevalue",
6
otherkey = 123
7
)
8
)
9
​
10
​
11
// Shorthand notation
12
do obj2 = {
13
"title": "toto",
14
"body": {
15
"somekey": "somevalue",
16
"otherkey": 123
17
}
18
}
19
​
20
say obj1.title // "toto"
21
say obj2.body.otherkey // 123
Copied!

Arrays

You can also iterate over an array (represented by items inside square brackets: ["a", "b", "c"]) with the foreach keyword, and access any of its items by using its index in square brackets notation: items[2].
1
do items = ["a", "b", "c"]
2
​
3
// iterate over all the elements in the array
4
foreach (elem, index) in items {
5
say "index: {{index}}, elem: {{elem}}"
6
}
7
​
8
say items[2] // "c"
Copied!
Note that foreach creates a copy of each item as it iterates over the array. So for example, in this next example, the array is not modified:
1
do items = ["a", "b", "c"]
2
​
3
foreach (item) in items {
4
do item = item.to_uppercase()
5
say item // "A", "B", "C"
6
}
7
​
8
say "{{items}}" // ["a", "b", "c"]
Copied!
You can modify the contents of an array either by assigning a new value to any of the items in the array, adding new items at the end with array.push(elem) or removing the last element with array.pop().
1
do items = ["a", "b", "c"]
2
​
3
do items = ["a", "b", "c"]
4
foreach (item, index) in items {
5
do items[index] = item.to_uppercase()
6
say item // "a", "b", "c": the copy has not been affected
7
say items[index] // "A", "B", "C": but the array content has been modified
8
}
9
​
10
say "{{items}}" // ["A", "B", "C"]: the array was modified
11
say items[2] // "C"
Copied!
You can also use Array methods to apply changes or get data from arrays.

Printing non-literal values

To print any value as a string, simply use the Text(value) component or use the curly-brace template syntax "{{value}}"
1
do items = ["a", "b", "c"]
2
​
3
// print a stringified version of the array
4
say "{{items}}"
5
​
6
// alternatively, you can also use the .to_string() array method
7
say items.to_string()
Copied!
Last modified 7mo ago