Collections

Workflow language provides two types for storing sets of values (known as collections), list and collection. Collections are always clear about the types of values that they can store. This means that you cannot insert a value of the wrong type into a collection by mistake. It also means you can be confident about the type of values you will retrieve from a collection.

Collections of type list are mutable meaning that you can change (or mutate) the collection after it’s created by adding, removing, or changing items. Items in a list can be read via a foreach loop or by their index. Collections of type collection on the other hand are immutable meaning that it's size and contents cannot be changed. Items in a collection can only be accesed via the foreach loop and not by their index. 

The list type

You can create an empty list of a certain type using this syntax:

var nameList as list of string;

The list defined in the example can only store items of type string, this is specified by using the of keyword folowed by the type of values the list can store.

Adding items to the list is done as follows:

var nameList as list of string; nameList[] = "John"; nameList[] = "Anna"; nameList[] = "Bob";

Adding items to a list is done by assigning a value to the list add indexer, written as [].

The following feature is avilable since version 1.5

Finding how many items are in the list is done as follows:

var nameList as list of string; //add items to list var theSize = nameList.size;

Every variable of type list has a property named size which contains the number of items in the list. This property cannot be assigned. 

Accesing items

Accesing items in a list is done via the indexer written as a pair of brackets ([index as int]) as in this example:

Notice that in Workflow language lists start at index zero and end at size - 1.

Removing items

The following feature is avilable since version 1.5

Removing items can be done as follows:

Removing items from a list is done by assigning the index to remove the list remove indexer, written as [-].

Clearing the list

Clearing the list is done by assiging the list to null as follows:

Multiple-dimensional or nested lists

The next example shows how to find the sum of the diagonal of following 3 by 3 matrix:

1

2

3

4

5

6

7

8

9

The collection type

Collection can only be created with a FETCH statement, or as a result of a function call. Collections created by function call are build outside the Workflow language via the implementation of an external workflow package.

Here is an example on how to create a collection with a FETCH statement:

When we declared the variable entities we didn't specify it's type. It's inferred by the compiler that the type is a collection of items that have a property key of type int, and a property of type string, in Workflow language this is called a dynamic type. Dynamic types are strong types, meaning that their type safety (in assignments, function calls, comparisons, expressions etc.) is validated at compile time.

The type of variable col is inferred by the compiler as the same as the return type of the function system->sequence() which is collection of int.

Duck Typing on collections

Duck typing on collections works in a similar way as it works on any complex type, the difference being that the type-checking occurs also on the type of the items stored in the collection.

The following example shows how to convert a dynamic type to a static type using duck typing: