Dodo doc > Variables and Functions > Variables


Variables

Syntax

The dodo language is case sensitive. Variable names always start with a lower case letter. They can contain letters, digits, underscore and apostrophe.

More characters are allowed if the variable name is preceded with a single underscore. The first character of the name must be a non-uppercase letter as defined by the Unicode standard.

Normal variable name characters and any character which is not in the ASCII range can be used for the rest. The dash and the underscore characters are equivalent. Note that the last character or the pair of third and fourth characters cannot be dashes or underscores.

Examples:

acceleration
last_node
lastNode
b'
_ønskeliste
_in-frame
Rationale: identifiers starting with upper case are reserved for types in the dodo language, hence the rule of starting with lower case for variables. The apostrophe is useful in mathematical applications. The extended syntax introduced with an underscore allows for more meaningful variable names, notably in countries with a non-latin alphabet.

Declaration

A variable declaration starts with keyword "def" or a type name followed by a variable name and an initialisation expression introduced by "=". If a type name is specified, the initialisation expression is optional. In that case the variable takes the default value for this type.

Examples:

def x = 3.41
def centre = (left + right) / 2
String address_1 = "(empty)"
MasterPlan plan
Rationale: the "def" keyword is used for local type inference which makes programmers life easy in a strongly typed language like dodo. Since every type has a default value, there is no general-purpose "null" value in dodo. This results in safer and more predictable program execution. When the equivalent of "null" is required dodo offers alternative mechanisms (namely abstract state, linked references and futures).

Use as argument

Once a variable is declared, it can be used and manipulated by passing it as argument to an operator or a function. By default a variable is passed by value: the function only sees the value held by the variable, and the function invocation has no effect on the variable itself.

To allow the function or operator to manipulate the variable, it should be passed by reference. This is done in dodo by prefixing the variable name with a dot. Affecting a new value to a variable, in particular, makes use of that notation. The "=" operator can mean either equality testing or affectation depending on the how its first operand is passed, by value or by reference.

Examples:

apples + oranges
sum(x, 2 * x', 3 * x'')
.velocity = distance / time
Mix(.fruits, 9)
choice = "Ok"

Note: by default, only local variables in a function and instance members in a type constructor can be passed by reference.

Rationale: pass by value as default encourages stateless programming, which makes code more robust and helps concurrency. The explicit annotation of variables passed by reference at the caller site documents the fact that the variable is likely to be altered by the call. Overloading the "=" sign gives a nice syntax to equality testing.

Use as prototype

The dodo type system is based on prototypes. Many language features rely on it.

The value of any variable can be used as prototype, or model, for a new variable. The new variable inherits the characteristics of the prototype but some attributes can take new values.

The "new" keyword creates a new variable based on an existing one. The variable name can be followed by a list of attributes with their new value. The expression can be followed by a block containing declarations.

Examples:

new prettyHouse(wallColour: yellow, windows: 6)
new apple: def size = medium.

Rationale: creating new variables which differ only by a few attributes allows to work easily with immutable variables. In turn, the use of immutable variables makes code more robust and helps concurrency. The ability to extend a variable with new declarations is a convenient feature of prototype-based programming.

^ 3. Variables and Functions

v 3.2. Constants and Enums