Constants
A value fixed for the run #
A constant is a name in capitals at the top level of a file, with a value after =. It never changes, and any function of the file can read it:
SECONDS_PER_DAY = 24 * 60 * 60
GREETING = "hello"
def main() -> Void
puts("#{GREETING}: #{SECONDS_PER_DAY}")
endhello: 86400A name is capital letters, digits and _, at least two characters (LIMIT, MAX_DEPTH). Another spelling is PARSE-CONSTANT-NAME:
MaxDepth = 8
def main() -> Void
puts(MaxDepth)
enderror[PARSE-CONSTANT-NAME]- Constants come after the imports and before types and functions, with no blank line between them.
- Give a type as in a binding (
RATIO: Float = 1.5). It also types[]and{}.
Build a table once #
A function that builds a list or a map runs the code each time it is called. A constant is built once, before main, and every read gives the same value:
PRICES = {"tea": 3, "cocoa": 5}
FRUITS = Set.new(["kiwi", "apple"])
def kind(name: String) -> String
return "fruit" if FRUITS.include?(name)
"drink"
end
def main() -> Void
puts(PRICES.get("cocoa", 0))
puts(kind("kiwi") + " " + kind("tea"))
end5
fruit drink- A constant works like any value:
PRICES.get(...),PRICES["tea"],FRUITS.size. - A constant may read another one, above or below it. A loop is
CHECK-CONSTANT-CYCLE.
When a constant is built #
- A literal such as
WIDTH = 8is part of the program. - Any other constant is built once, before
main, and only if a function thatmaincan reach reads it. An unread constant never runs. - If an initializer fails, the program stops before
mainprints anything:
BAD = [1][5]
def main() -> Void
puts("never printed")
puts(BAD)
enderror[RUNTIME-INDEX-OUT-OF-RANGE]The stack trace names BAD.
What a constant may hold #
- It may hold literals, lists, maps, sets, struct and enum values, operators, other constants and builtins without effects (
Set.new,map,reduce, …). - A call of your own function, a function value, a
whileloop or a builtin that reads or writes the world isCHECK-CONSTANT-EXPRESSION:
LINE = IO.gets!()
def main() -> Void
puts(LINE)
enderror[CHECK-CONSTANT-EXPRESSION]- Its type is numbers, text, lists, maps, sets, ranges,
Result, or structs and enums of those. A channel, task or function isCHECK-CONSTANT-TYPE. The one exception is a whole initializerATS.new(), a table that every task shares (a constant table). - Nothing assigns to a constant:
LIMIT = 2in a function isPARSE-INVALID-ASSIGNMENT-TARGET.
Share a constant between files #
A constant above private (or in a file with no private) is public, and the importing file brings it in by name, like a function or a struct:
MAX = 100
NAMES = ["x", "y"]import {MAX, NAMES as LABELS} from "limits"
def main() -> Void
puts(MAX)
puts(LABELS.size)
end100
2A file that imports the module under an alias reads the constant as Limits.MAX, like an exported function:
import Limits from "limits"
def main() -> Void
puts(Limits.MAX)
puts(Limits.NAMES.size)
end100
2A constant the module keeps private is CHECK-IMPORT-NAME, as for a function.