// Build: build/flecha build examples/const_declarations.fl -o build/const_example
// Run: build/const_example
// Prints the values described below and exits with 42.
const i32 add(const i32 input) {
// Parameters arrive initialized; const prevents reassignment here.
return input + increment;
const string echo(const string text) {
// Owning strings keep their existing copy semantics.
// none takes the return type's zero value.
// Const initialization can call a function at runtime.
const i32 answer = add(40);
// Reading a const value does not make its copy const.
// A const return type does not make an inferred binding const either.
stdoutln(returned); // 44
// Both cast spellings accept const-qualified types.
const i64 wide = answer as const i64;
const u8 byte = answer.cast<const u8>;
const f64 fraction = 1.5;
const bool same = wide == (byte as i64);
stdoutln(fraction); // 1.5
// A nested mutable binding can shadow a const binding.
stdoutln(answer); // 42: the outer binding
// Explicit zero initialization also works with const declarations.
const bool disabled = none;
const string emptyText = none;
stdoutln(disabled); // false
stdoutln(emptyText.len()); // 0
// none also works in assignment, function arguments, and returns.
stdoutln(reset + add(none) + zero()); // 2
// Copying a const owning string produces an independent mutable owner.
const string original = echo("Flecha");
const stringv view = original.view();
stdoutln(view); // Flecha
stdoutln(mutableCopy); // Changed
// constval is an ordinary identifier, not a qualifier or keyword.
stdoutln(constval); // 43