Working with Variables & Expressions
Working with Variables & Expressions
Introduction
Like Python, Colang supports these fundamental data types: string, int, float, bool, list, set, dict, in addition to the Colang specific event, action and flow references. We have already seen how to assign event, action and flow references to a variable using the ... as $ref notation. Here $ref is also just a variable. To enable whitespace characters in flow names, Colang variables must always start with the $ character and cannot contain whitespace characters themselves.
Variable naming convention:
- Has to start with an alphabetic character
- Can contain alphanumeric characters including
_ - Is case sensitive
Variables in Colang behave exactly like in Python. As a consequence, mutable data types like a list will not be copied for a new variable assignment but only referenced by the new variable.
variables/assignment/main.co
Note that Colang references are also mutable and therefore point to the same underlying object:
variables/references/main.co
Currently, the creation of references of other variables is not supported. References can only point to event, action or flow objects and are created in send, match, start or await statements using the as keyword.
- Assignment to variables will always create a copy of the value in memory
- Copy of event, action or flow references keep pointing to the same object
- Flow parameters are passed by value
Here are some variable assignment examples:
Expression Evaluation
Colang supports evaluation of common Python expressions for simple and compound data types (see Simple Eval):
Here is how expression can be used within Colang:
You see how expressions can be used in different context and need to be wrapped in parentheses if used as a standalone statement or as a flow parameter.
Flow Variable Access
By default, variables defined in a flow have a local scope and are not accessible from outside the flow. One way to enable access to them is by declaring them as flow attributes using the notation shown in Defining Flows in the flow definition:
variables/flow_attributes/main.co
With this, we can access, for example, the user transcript and use it to repeat it with a bot utterance action.
Another way to share information between flows using variables is to make them global by using the keyword global.
variables/global_variables/main.co
As you can see from the example, we need to define in each flow that the variable $transcript is global in order to get access to the global instance. Otherwise, it would be a local variable hiding the global instance. But please think twice about using global variables as it can be an indication of a non-optimal Colang design.
Expressions in Strings
As in Python’s formatted string literals, we can use braces to evaluate an expression inside a string "{$variable}":
variables/string_expression_evaluation/main.co
If you need to include a brace character in the literal text, it can be escaped by doubling: {{ and }}.
Flow Member Variables
To access a flow instance’s member variables, you can use a reference or the reserved variable $self from within the flow itself:
Note that you should not change those values if you are not sure what you are doing since this can have side effects on the further execution of the flow.
Action Member Variables
To access the member variables of an action, you can use an action reference:
Event Member Variables
To access the member variables of an event, you can use an event reference:
System and Bot Configuration Values
To access system or bot specific configuration variables, you can use $system:
As an example, if you defined a new boolean value streaming in the yaml bot configuration:
you can access and print it like this:
Next, we learn how to use Flow control to create branching or looping interaction patterns.