Data Types
Kode has a comprehensive type system with both primitive and composite types.
Primitive Types
Integer (int)
kode
let age: int = 30
let negative: int = -15Float (float)
kode
let pi: float = 3.14159
let temp: float = -2.5Boolean (bool)
kode
let isActive: bool = true
let isDone: bool = falseString (string)
kode
let name: string = "John Smith"
let message: string = "Hello, world!"Null
kode
let empty = nullComposite Types
Arrays
kode
let numbers: [int] = [1, 2, 3, 4, 5]
let names: [string] = ["Alice", "Bob", "Charlie"]
let mixed: [any] = [1, "two", true, 4.5]Tuples
kode
let pair: (int, string) = (42, "answer")
let triple: (int, float, bool) = (10, 3.14, true)Maps / Dictionaries
kode
let person = {
"name": "Alice",
"age": 30,
"email": "alice@example.com"
}
let scores: {string: int} = {
"alice": 95,
"bob": 87,
"charlie": 92
}Function Types
kode
let add: (int, int) -> int = fn(a, b) { return a + b }
let greet: (string) -> string = fn(name) { return "Hello, " + name }Generic Types
kode
let container: Container<int> = makeContainer(42)
let list: List<string> = ["a", "b", "c"]
let result: Result<int, Error> = divide(10, 2)Type Checking
Use the type() built-in function to check types at runtime:
kode
let x = 42
print(type(x)) // "int"
let name = "Alice"
print(type(name)) // "string"
let arr = [1, 2, 3]
print(type(arr)) // "array"Type Conversions
Kode provides built-in functions for type conversion:
kode
let x = 42
let s = string(x) // "42"
let f = float(x) // 42.0
let s2 = "123"
let i = int(s2) // 123
let b = bool(1) // true