Variables & let
Variables & let
Declaring Variables
Use let to create a new variable binding. Thagore uses type inference — you don’t need to specify the type if it can be inferred:
let x = 42 # i32 (inferred)let name = "Alice" # String (inferred)let pi = 3.14 # f32 or f64 (inferred)let flag = true # bool (inferred)Explicit Type Annotations
You can specify types explicitly in function parameters and struct fields:
func add(a: i32, b: i32) -> i32: return a + b
struct Config: width: i32 height: i32 name: StringSupported Primitive Types
| Type | Description | Example |
|---|---|---|
i32 | 32-bit signed integer | let x = 42 |
f32 | 32-bit float | let x = 3.14 |
f64 | 64-bit float | let x = 3.14159265 |
String | UTF-8 string | let s = "hello" |
bool | Boolean (true/false) | let b = true |
ptr | Raw pointer | let p = __thg_ptr_null() |
void | No return value | Used in extern func |
Reassignment
Variables declared with let can be reassigned:
func main() -> i32: let x = 1 let y = 2 let z = x + y x = z # reassign x to the value of z print(x) # Output: 3 return 0Scope Rules
Variables are scoped to their enclosing block (function body, if/else block, while body, etc.):
func main() -> i32: let x = 10 if (x > 5): let y = 20 # y is scoped to this if-block print(y) # y is not accessible here print(x) # x is still accessible return 0String Variables
Strings support concatenation with +:
let first = "Hello"let space = " "let world = "Thagore"let greeting = first + space + worldprint(greeting) # Output: Hello ThagoreString Comparison
Strings can be compared with == and !=:
let a = "hello"let b = "hello"if (a == b): print("Strings are equal!")Boolean Values
let active = truelet done = false
if (active): print("Active!")Arrays
Fixed-size arrays are declared with [type; size] syntax:
# Declare and initialize an arraylet nums = [10, 20, 30, 40]
# Access by indexprint(nums[2]) # Output: 30
# Modify by indexnums[0] = 999
# Pass to functionsfunc sum_array(arr: [i32; 4]) -> i32: let i = 0 let total = 0 while (i < 4): total = total + arr[i] i = i + 1 return total
let s = sum_array(nums)print(s)