Next: , Previous: , Up: MFL   [Contents][Index]


4.11 Constants

A constant is a symbolic name for an MFL value. Constants are defined using const statement:

[qualifier] const name expr

where name is an identifier, and expr is any valid MFL expression evaluating immediately to a constant literal or numeric value. Optional qualifier defines the scope of visibility for that constant (see scope of visibility): either public or static.

Once defined, any appearance of name in the program text is replaced by its value. For example:

const x 10/5
const text "X is "

defines the numeric constant ‘x’ with the value ‘5’, and the literal constant ‘text’ with the value ‘X is ’.

A special construct is provided to define a series of numeric constants (an enumeration):

[qualifier] const
do
  name0 [expr0]
  name1 [expr1]
  ...
  nameN [exprN]
done

Each exprN, if present, must evaluate to a constant numeric expression. The resulting value will be assigned to constant nameN. If exprN is not supplied, the constant will be defined to the value of the previous constant plus one. If expr0 is not supplied, 0 is assumed.

For example, consider the following statement

const
do
  A
  B
  C 10
  D
done

This defines ‘A’ to 0, ‘B’ to 1, ‘C’ to 10 and ‘D’ to 11.

As a matter of fact, exprN may also evaluate to a constant string expression, provided that all expressions in the enumeration ‘const’ statement are provided. That is, the following is correct:

const
do
  A "one"
  B "two"
  C "three"
  D "four"
done

whereas the following is not:

const
do
  A "one"
  B
  C "three"
  D "four"
done

Trying to compile the latter example will produce:

mailfromd: filename:5.3: initializer element is not numeric

which means that mailfromd was trying to create constant ‘B’ with the value of ‘A’ incremented by one, but was unable to do so, because the value in question was not numeric.

Constants can be used in normal MFL expressions as well as in literals. To expand a constant within a literal string, prepend a percent sign to its name, e.g.:

echo "New %text %x" ⇒ "New X is 2"

This way of expanding constants creates an ambiguity if there happen to be a variable of the same name as the constant. See variable--constant clashes, for more information of this case and ways to handle it.


Next: , Previous: , Up: MFL   [Contents][Index]