Dodo doc > Variables and Functions > Constants and Enums > Constants


Constants

Syntax

Any variable name starting with a lower case letter or underscore and a non-capital letter can be a constant name. In addition, constant names can start with a single underscore followed by a capital letter then more underscores, capital letters or digits.

Examples:

defaultWidth
_stützpunkt
_MAX_DURATION
Rationale: there is not much distinction between a variable and a constant in dodo, so their names can be the same. The all-capital syntax is intended to ease the translation of C programs to dodo.

Declaration

A constant is declared like a variable, except that empty brackets "()" are added after the constant name. A constant always requires an initialisation value. To specify its type, replace the empty brackets with an arrow and "get" followed by a type.

Examples:

def step() = 4
def filename -> get(String) = "scores"
Rationale: as a constant is a variable that cannot be assigned a value, it is implemented in dodo as a function without parameters. The compiler can optimise the code to replace the function call with its returned value, or cache the result of the function call if the returned value is unknown. In the second syntax, the empty brackets are optional and "get" is actually the return continuation which requires a type.

Use as argument

A constant can be passed by value to a function like any variable. A constant cannot be passed by reference.

Examples:

level.increase(step)
2 * pi
Rationale: even though it looks like a variable, this is really a function call for which the brackets are optional. Since the same applies to all functions without parameters, the drawback is that constants cannot be distinguished from no-parameter functions.

Use as prototype

Constants can be used as prototype in exactly the same way as other variables.


^ 3.2. Constants and Enums

v 3.2.2. Enum Variables