The basics

In this chapter the basic features of the Workflow language are described.

Variables

Variables associate a name (such as maximumNumberOfLoginAttempts or welcomeMessage) with a value of a particular type (such as the number 10 or the string "Hello"). The value of a variable can be set to a different value in the future.

Declaring variables

Variables must be declared before they are used. You declare variables with the var keyword. Here’s an variables can be used to track the number of login attempts a user has made:

var currentLoginAttempt = 0;

This code can be read as:

“Declare a new variable called currentLoginAttempt, and give it an initial value of 0.”

Variable Types

You can provide a variable type when you declare a variable, to be clear about the kind of values the variable can store. Write the keyword as, followed by a space, followed by the name of the type to use.

This example provides a variable type for a variable called welcomeMessage, to indicate that the variable can store String values:

var welcomeMessage as string;

The code above can be read as:

“Declare a variable called welcomeMessage that is of type string.”

The phrase “of type string” means “can store any string value.” Think of it as meaning “the type of thing” (or “the kind of thing”) that can be stored.

The welcomeMessage variable can now be set as follows:

welcomeMessage = "Hello";

Naming Variables

Variable names can contain only the characters from a to z (and A to Z), underscore and numbers from 0 to 9 (but they cannot begin with a number).

Once you’ve declared a variable of a certain type, you can’t redeclare it again with the same name, or change it to store values of a different type.

Note

If you need to give a variable the same name as a reserved Workflow keyword, surround the keyword with backticks (`) when using it as a name. However, avoid using keywords as names unless you have absolutely no choice.

Comments

Use comments to include nonexecutable text in your code, as a note or reminder to yourself. Comments are ignored by the Workflow compiler when your code is compiled.

Comments in Workflow are very similar to comments in C. Single-line comments begin with two forward-slashes (//):

Multiline comments start with a forward-slash followed by an asterisk (/*) and end with an asterisk followed by a forward-slash (*/):

Basic Types

integer

Integers are whole numbers with no fractional component, such as 42 and -23. Integers are either signed (positive, zero, or negative) or unsigned (positive or zero).

The value of an integer ranges between -2E63, and 2E63-1.

decimal

decimal are numbers with a fractional component, such as 3.141590.1, and -273.15.

Decimal type can represent a much wider range of values than the integer type, and can store numbers that are much larger or smaller than can be stored in an integer.

datetime

datetime type holds, as the name suggests, a date and a time including the timezone.

string

string is a series of characters, such as "hello, world" or "albatross". Workflow strings are represented by the string type. 

boolean

Workflow has a basic Boolean type, called boolean. Boolean values are referred to as logical, because they can only ever be true or false. Workflow provides two Boolean constant values, true and false:

Complex Types

Complex Types are sets of Basic Types or Complex Types defining a new Complex Type given a unique name. You cand use this name as a variable type later in your code.

Here's an example of a Complex Type called car:

You can declare variables with the types defined, for example:

The code above can be read as:

“Declare a variable called myCar that is of (complex) type car.”

Automatic Instantiation and Null Coalescing

In Workflow, in contrast to objectual languages such as C#, Java or C++ types do not need to be instantiated in order to be used. Because types are not objects (like in C#, Java or C++) and cannot have internal state or private fields, they are automaticaly instantiated.

The folowing example shows how a variable is automaticaly instantiated on use:

Automatic instantiation happens only when you try to set a value to a property in contrast, when reading an uninitialized (or null) property the default value (or null) is returned.

If you are trying to read a chain o properties and one property value, when reading from left to right, is null then null is returned, for example:

The behaviour described above is called Null Coalescing.

Type Safety, Type Inference and Duck Typing

Workflow is a type-safe language. A type safe language encourages you to be clear about the types of values your code can work with. If part of your code expects a string, you can’t pass it an int by mistake.

Because Workflow is type safe, it performs type checks when compiling your code and flags any mismatched types as errors. This enables you to catch and fix errors as early as possible in the development process.

Type-checking helps you avoid errors when you’re working with different types of values. However, this doesn’t mean that you have to specify the type of every variable that you declare. If you don’t specify the type of value you need, Workflow uses type inference to work out the appropriate type. Type inference enables a compiler to deduce the type of a particular expression automatically when it compiles your code, simply by examining the values you provide.

Because of type inference, Workflow requires far fewer type declarations than languages such as C or Java. Variables are still explicitly typed, but much of the work of specifying their type is done for you.

Type inference is particularly useful when you declare a variable with an initial value. This is often done by assigning a literal value (or literal) to the variable at the point that you declare it. (A literal value is a value that appears directly in your source code, such as 42 and 3.14159 in the examples below.)

For example, if you assign a literal value of 42 to a new variable without saying what type it is, Workflow infers that you want the variable to be an int, because you have initialized it with a number that looks like an integer:

Likewise, if you don’t specify a type for a floating-point literal, Workflow infers that you want to create a decimal:

If you combine integer and floating-point literals in an expression, a type of decimal will be inferred from the context:

The literal value of 3 will have de inferred type of integer and 0.14159 will be inferred as decimal wich is a superset of integer thus the whole expression will be inferred as decimal.

Duck-typing is a technique of automatically converting between different types if certain similarity requirements are met. The similarity requirements are called "Duck test" and, as Wikipedia describes it, this means: "If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck.". For more information check Type conversions article.