Global Constants¶
Define values at the module level that can be accessed anywhere in your code.
Basic Syntax¶
let MAX_USERS = 100
let APP_NAME = "MyApp"
let PI = 3.14159
let DEBUG = true
def main() -> int:
print(f"App: {APP_NAME}")
print(f"Max: {MAX_USERS}")
return 0
Naming Convention¶
Use UPPER_CASE for globals:
# ✓ Good
let MAX_SIZE = 1024
let API_KEY = "secret"
# ✗ Warning
let myConst = 42 # Warning: should be UPPER_CASE
Underscore prefixes are also valid:
Immutability¶
Globals are always immutable:
Visibility¶
Only pub globals can be imported:
# config.desi
pub let MAX_RETRIES = 3 # Exported
let INTERNAL_KEY = "abc" # Private
let _SECRET = "hidden" # Private (underscore convention)
# main.desi
from config import MAX_RETRIES # Works
from config import INTERNAL_KEY # Error: not public
Either import form reaches an exported constant:
Supported Types¶
# Integers
let INT_VAL = 42
let NEG_VAL = -100
# Floats
let FLOAT_VAL = 3.14
# Booleans
let BOOL_VAL = true
# Strings
let STR_VAL = "Hello"
Computed Values¶
An initializer may be arithmetic over numeric literals, or + over string
literals. It is folded at compile time, so the constant is usable from other
modules like any other:
let SECONDS_PER_DAY = 60 * 60 * 24 # 86400
let TIMEOUT_MS: int = 30 * 1000 # 30000
let AREA = (2 + 3) * 10 # 50
let RATIO = 1.5 * 2.0 # 3.0
let BANNER = "desi " + "0.1.0" # "desi 0.1.0"
Anything that has to run to produce its value — a function call, a constructor — is not a constant expression:
Call the function where you use it
A global initialized by a function call is only initialized when the module
that declares it runs. Importing such a constant does not run it, and the
link fails with an undefined symbol. Put the call in a function and call it
from main, or write the value out as a literal.
Using in F-Strings¶
let APP_NAME = "Desi"
pub let VERSION = "0.1.0"
let _BUILD = 123
def main() -> int:
print(f"{APP_NAME} v{VERSION} (build {_BUILD})")
return 0
Best Practices¶
| Do | Don't |
|---|---|
| Use for configuration | Use for mutable state |
| Keep values simple | Define complex objects |
Use pub for exported values |
Assume visibility |
Use UPPER_CASE or _UPPER_CASE |
Use camelCase |