module Const {
// MODULE SCOPED CONSTANTS
// use const at modules scope to define a named constant value. the value must
// be a constant expression and will not change during the program execution
const PI = 3.14;
// the constant may optionally have a type
const SMALL_PI : float = 3.14;
// constants can include data structures, including ref structures
// define a ref tree node
ref struct Node {
val : int;
left : Node? = none;
right: Node? = none;
}
// define node with two sub-nodes
const N1 = Node(
2,
Node(1),
Node(3)
);
// define a node that references other constant node
const N2 = Node(
4,
N1
);
// FUNCTION SCOPED (LOCAL) CONSTANTS
// use const within a function to define a local variable that does not change
// after it has been assigned. the syntax is the same as with let.
// the value of the constant need not be a compile-time constant expression.
fn test1() {
const x = 1; // x may not be assigned to
}
// TYPE SCOPED CONSTANTS
// use const with a type name to associate a constant with a type
const Node.ZERO = Node(0);
fn test2() {
const node = Node.ZERO;
}
}