Skip to content

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:

variables.tg
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:

typed_params.tg
func add(a: i32, b: i32) -> i32:
return a + b
struct Config:
width: i32
height: i32
name: String

Supported Primitive Types

TypeDescriptionExample
i3232-bit signed integerlet x = 42
f3232-bit floatlet x = 3.14
f6464-bit floatlet x = 3.14159265
StringUTF-8 stringlet s = "hello"
boolBoolean (true/false)let b = true
ptrRaw pointerlet p = __thg_ptr_null()
voidNo return valueUsed in extern func

Reassignment

Variables declared with let can be reassigned:

reassignment.tg
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 0

Scope Rules

Variables are scoped to their enclosing block (function body, if/else block, while body, etc.):

scope.tg
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 0

String Variables

Strings support concatenation with +:

strings.tg
let first = "Hello"
let space = " "
let world = "Thagore"
let greeting = first + space + world
print(greeting) # Output: Hello Thagore

String Comparison

Strings can be compared with == and !=:

string_compare.tg
let a = "hello"
let b = "hello"
if (a == b):
print("Strings are equal!")

Boolean Values

booleans.tg
let active = true
let done = false
if (active):
print("Active!")

Arrays

Fixed-size arrays are declared with [type; size] syntax:

arrays.tg
# Declare and initialize an array
let nums = [10, 20, 30, 40]
# Access by index
print(nums[2]) # Output: 30
# Modify by index
nums[0] = 999
# Pass to functions
func 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)