Advanced Features
Advanced Features
This page covers Thagore’s advanced language features that go beyond basic syntax.
Enums
Define algebraic data types with enum:
enum Color: Red Green Blue
enum Shape: Circle(f32) Rectangle(f32, f32)Enums with payloads (data attached to variants) are tracked by the compiler’s feature counter system.
Match Expressions
Pattern match on enum variants:
enum Direction: North South East West
func describe(d: Direction) -> String: match d: North: return "going up" South: return "going down" East: return "going right" West: return "going left"Type Aliases
Create type aliases with the type keyword:
type Distance = f32type Name = StringTraits and impl for
Define interfaces with trait and implement them:
trait Printable: func display(self) -> String
struct Point: x: i32 y: i32
impl Printable for Point: func display(self) -> String: return "Point"Visibility: pub
Mark declarations as public with pub:
pub func api_function() -> i32: return 42
pub struct PublicConfig: name: Stringunsafe Blocks
Mark code that requires unsafe operations:
unsafe: let raw = malloc(1024) # raw pointer operations free(raw)defer
Schedule cleanup code that runs when the function exits:
func process_file() -> i32: let handle = fs.open_read("data.txt") defer fs.close(handle) # ... process the file ... return 0 # fs.close(handle) runs here automaticallycomptime — Compile-Time Evaluation
Execute code at compile time:
comptime: let size = 4 * 1024 # compile-time evaluatedThe compiler tracks comptime blocks and evaluates them during the lowering phase via lowering/transform/comptime/eval.tg.
Closures
Lambda-style anonymous functions:
# Closure syntax is parsed and tracked# Full closure semantics are in developmentTuple Destructuring
# let (a, b) = get_pair()# Parsed and tracked by the compilerArray Literals and Slicing
let nums = [1, 2, 3, 4, 5]
# Array slice (tracked feature)# let sub = nums[1..3]Loop Labels
# Named loops for break/continue targeting# outer: while (true):# inner: while (true):# break outerResult Sugar
# Error handling with ? operator# let value = try_parse(input)?Raw Strings
# Raw strings with r# prefix# let pattern = r#"no escapes \n here"#Interpolated Strings
# String interpolation with v"..." syntax# let msg = v"Hello {name}, you are {age} years old"Intent System
Thagore’s unique intent system allows declarative, constraint-based programming:
intent func fib(n: i32) -> i32: goal: auto_plan constraints: deterministic == true
func fib(n: i32) -> i32: if (n < 2): return n return fib(n - 1) + fib(n - 2)
func main() -> i32: print(fib(35)) return 0Intent Block Structure
intent func <name>(<params>) -> <type>: goal: <goal_name> strategy: <strategy> (optional) examples: <assertion expressions> constraints: <constraint expressions>Supported Goals
| Goal | Description |
|---|---|
auto_plan | Compiler automatically selects an optimization strategy |
reduce_sum | Optimize for sum reduction patterns |
off | Disable intent optimization for this function |
Intent with Examples and Constraints
intent loop i in 0..10: goal: reduce_sum examples: reduce_sum([1,2,3]) == 6 constraints: deterministic == true time <= O(n)Feature Detection Summary
The compiler tracks usage of advanced features through its AST parser. Here are all tracked features:
| Feature | Compiler Field | Status |
|---|---|---|
| Enum payloads | enum_payload_count | Parsed |
| Match expressions | match_count | Parsed + validated |
| Range loops | range_loop_count | Parsed + validated |
| If expressions | if_expr_count | Parsed |
| Closures | closure_count | Parsed |
| Unsafe blocks | unsafe_count | Parsed |
| Extension impl | extension_impl_count | Parsed |
| Pub visibility | visibility_count | Parsed |
| Tuple destruct | tuple_destruct_count | Parsed |
| Array literals | array_literal_count | Parsed |
| Slice expressions | slice_expr_count | Parsed |
| Loop labels | loop_label_count | Parsed |
| Raw strings | raw_string_count | Parsed |
| Interpolated strings | interpolated_string_count | Parsed |
| Result sugar | result_sugar_count | Parsed |
| Defer | defer_scope_count | Parsed |
| Comptime | comptime_count | Parsed |