Global Variables

    Global variables are declared at the top-level scope. They are accessible throughout the module or package, depending on their visibility modifier.

    Visibility

    • Private: Declared without a modifier; accessible only within the current module.
    • Public (pub): Accessible from other modules or files.
    const VERSION = "1.0"; // private, immutable
    
    pub const COUNTER = 0; // public, immutable
    

    Mutability

    • const: Immutable after declaration. Attempting to reassign will cause a compile error.
    • var: Mutable; the value can be changed throughout the program lifecycle.
    const PI = 3.14159;
    
    PI = 3.14; // ERROR!
    

    External Symbols

    Use the extern keyword to declare symbols defined in external libraries or other translation units. These do not include an initializer.

    extern errno: int; // private external symbol
    
    pub extern stdin: void*; // public external symbol
    

    Local Variables

    Local variables are declared within function bodies using var or const. Their scope is restricted to the block in which they are defined.

    pub fn main() {
        var name = "Cyrus"; // mutable local
    
        const timeout_ms = 500; // immutable local
    }
    

    If you need to ensure a specific type, you can annotate the declaration:

    pub fn main() {
        const epsilon: float64 = 0.001;
    
        var retries: int;
    }
    

    Zero Initialization

    In Cyrus, any variable declared without an explicit initial value is automatically zero-initialized.

    pub fn main() {
        var x: int; // initialized to 0
        var y: float64; // initialized to 0.0
        var z: void*; // initialized to (nil)
    
        printf("%d\n", x);
    }
    

    No Late Type Inference

    Cyrus does not allow late type inference. Every variable must have its type known at the point of declaration, either:

    • Via an initializer (expression has a known type)
    • Via an explicit type annotation

    Declaring a variable without a type and without an initializer is not allowed, even if you assign to it later:

    var x; // ERROR!
    
    x = 10;
    

    You must either write:

    var x = 10; // type inferred as int
    

    Or:

    var x: int; // explicit type, zero-initialized to 0
    
    x = 10;