Skip to content

Advanced Features

Advanced Features

This page covers Thagore’s advanced language features that go beyond basic syntax.

Enums

Define algebraic data types with enum:

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

match.tg
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_alias.tg
type Distance = f32
type Name = String

Traits and impl for

Define interfaces with trait and implement them:

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

visibility.tg
pub func api_function() -> i32:
return 42
pub struct PublicConfig:
name: String

unsafe Blocks

Mark code that requires unsafe operations:

unsafe.tg
unsafe:
let raw = malloc(1024)
# raw pointer operations
free(raw)

defer

Schedule cleanup code that runs when the function exits:

defer.tg
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 automatically

comptime — Compile-Time Evaluation

Execute code at compile time:

comptime.tg
comptime:
let size = 4 * 1024
# compile-time evaluated

The compiler tracks comptime blocks and evaluates them during the lowering phase via lowering/transform/comptime/eval.tg.

Closures

Lambda-style anonymous functions:

closures.tg
# Closure syntax is parsed and tracked
# Full closure semantics are in development

Tuple Destructuring

tuple_destruct.tg
# let (a, b) = get_pair()
# Parsed and tracked by the compiler

Array Literals and Slicing

array_slice.tg
let nums = [1, 2, 3, 4, 5]
# Array slice (tracked feature)
# let sub = nums[1..3]

Loop Labels

loop_labels.tg
# Named loops for break/continue targeting
# outer: while (true):
# inner: while (true):
# break outer

Result Sugar

result_sugar.tg
# Error handling with ? operator
# let value = try_parse(input)?

Raw Strings

raw_strings.tg
# Raw strings with r# prefix
# let pattern = r#"no escapes \n here"#

Interpolated Strings

interpolated.tg
# 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_demo.tg
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 0

Intent Block Structure

intent func <name>(<params>) -> <type>:
goal: <goal_name>
strategy: <strategy> (optional)
examples:
<assertion expressions>
constraints:
<constraint expressions>

Supported Goals

GoalDescription
auto_planCompiler automatically selects an optimization strategy
reduce_sumOptimize for sum reduction patterns
offDisable intent optimization for this function

Intent with Examples and Constraints

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

FeatureCompiler FieldStatus
Enum payloadsenum_payload_countParsed
Match expressionsmatch_countParsed + validated
Range loopsrange_loop_countParsed + validated
If expressionsif_expr_countParsed
Closuresclosure_countParsed
Unsafe blocksunsafe_countParsed
Extension implextension_impl_countParsed
Pub visibilityvisibility_countParsed
Tuple destructtuple_destruct_countParsed
Array literalsarray_literal_countParsed
Slice expressionsslice_expr_countParsed
Loop labelsloop_label_countParsed
Raw stringsraw_string_countParsed
Interpolated stringsinterpolated_string_countParsed
Result sugarresult_sugar_countParsed
Deferdefer_scope_countParsed
Comptimecomptime_countParsed