Variables & Scopes
We use variables to store values that can be used throughout the lifetime of a session. Every variable can only be used in the scope in which it was delcared.
Variable types
Section titled “Variable types”The USSD language supports 4 primitive types for variables
- Strings
- Numbers
- Booleans
- Lists
How to create a variable
Section titled “How to create a variable”We provide 3 ways of creating or getting values into variables.
1. Defining a Global Variable
Section titled “1. Defining a Global Variable”Using the define keyword at the top of your source, you can define a variable with a string content
2. Return Value from an Effect
Section titled “2. Return Value from an Effect”The return value from an effect call can hold variables of the types String, Number, Boolean or a List.
Do identify a variable as holding a list value, we have to wrap it in a square bracket. eg. [variableName]
2.1 Marking an Effect Variable as a List
Section titled “2.1 Marking an Effect Variable as a List”When a variable coming from an effect is wrapped in square brackets like [cars], it tells the compiler this variable cars contains a list.
Note: the compiler will allow you to only use a list variable with the list: action.
3. An Action that takes user input into a variable
Section titled “3. An Action that takes user input into a variable”We have 2 actions that can take user input and store them into a variable which are the prompt: action and the list: action.
The prompt action takes in a string input and stores it into a variable. illustrated below
The list action shows the user a list and asks them to pick an item, two variables are then populated with the user’s pick to hold the id and view of their pick. illustrated below
on line 3, the list action renders a list of cars to the user and waits for the user to select one of them. The selection of the user is then used to populate the two variables on the right. The id and view of what the user selected.