Types
Source: examples/types.ion in the repo.
// Type annotations and type checking
// Basic type annotations on let bindingslet x: int = 42;let pi: float = 3.14159;let name: string = "Ion";let active: bool = true;let items: list = [1, 2, 3];let config: dict = #{port: 8080};
io::println(f"x = {x}, pi = {pi}, name = {name}");io::println(f"active = {active}, items = {items}");
// Generic type annotations (outer type checked, inner is documentation)let maybe: Option<int> = Some(42);let result: Result<string, string> = Ok("success");let nums: list<int> = [1, 2, 3];let lookup: dict<string, int> = #{a: 1, b: 2};
io::println(f"\nOption: {maybe}");io::println(f"Result: {result}");io::println(f"Typed list: {nums}");io::println(f"Typed dict: {lookup}");
// The "any" type accepts everythinglet anything: any = "could be anything";io::println(f"Any: {anything}");
// fn type for function valueslet double: fn = |x| x * 2;io::println(f"double(5) = {double(5)}");
// Type checking with type_oflet values = [42, 3.14, "hello", true, [1], #{}, (1, 2), None, Ok(1)];for v in values { io::println(f" {v} -> {type_of(v)}");}
// Type annotations catch mismatches at binding timefn safe_bind(value) { try { // Using match for runtime type dispatch match type_of(value) { "int" => io::println(f" {value} is an int"), "string" => io::println(f" {value} is a string"), t => io::println(f" {value} is a {t}"), } } catch e { io::println(f" Error: {e}"); }}
io::println("\nType checking:");safe_bind(42);safe_bind("hello");safe_bind([1, 2]);