Ty Language Tutorial

The second layer of the Tyre language ecosystem

Ty extends T with generics, callable tuples, and impl blocks. It compiles down to T, like C++ compiles to C.

Hello World

print("hello world");

Ty shares T's syntax for the basics: print, top-level statements (no fn main needed), and all primitive types.

Functions

type n = i32;
fn twice n -> i32 {
    n.i32 * 2
}

type a = i32;
type b = i32;
fn max a & b -> i32 {
    if a.i32 > b.i32 { a.i32 } else { b.i32 }
}

print(twice(5), max(3, 7));

Output:

10 7

Functions work like in T. The last expression in the body is the return value. if/else is an expression.

Newtypes

Ty inherits T's newtype system. A newtype creates a distinct type, not an alias:

type meters = f64;
type seconds = f64;

let d = meters: 42.0;
let t = seconds: 1.5;
print(d, t);

Output:

42 1.5

meters and seconds are different types, even though both wrap f64. The type name is the constructor: meters: 42.0.

Tuples

Combine multiple newtypes into a compound type. Fields are identified by their type name:

type x, y = f32;
type Point = x & y;

let p = Point { x: 3.0, y: 4.0 };
print(p.x, p.y);

Output:

3 4

p.x returns a value of type x. type x, y = f32; defines both x and y as distinct f32 newtypes. Constructing a type with { } always produces data. Only calling a type with () triggers evaluation (requires an impl block).

Callable Tuples

This is Ty's central idea. A function definition:

type n = i32;
fn twice n -> i32 { n.i32 * 2 }

is sugar for a type definition plus an impl block:

// fn twice n -> i32 { n.i32 * 2 }
// is sugar for:
type n = i32;
type twice = n;
impl twice -> i32 { n.i32 * 2 }

print(twice(5));

Output:

10

The impl block makes the type callable. When you write twice(5), Ty constructs the tuple and evaluates it because twice has an impl -> i32. This follows Lisp's evaluation model: construction with impl = eval, brace construction = quote.

it refers to the implicit parameter inside an impl block. it.n accesses the field n. But since each field becomes a function parameter, you can also use the field name directly: n.i32 * 2 instead of it.n.i32 * 2. Use it when you need the whole value (e.g. passing it to another function).

Each type has at most one impl block. There are no methods — only callable types.

Multi-Field Impl

Callable tuples with multiple fields work the same way:

type a = i32;
type b = i32;
type add = a & b;
impl add -> i32 { a + b }

print(add(3, 7));

Output:

10

add(3, 7) constructs add { a: 3, b: 7 } and evaluates the body. Arguments are matched to fields by type.

Array Callables

When multiple values have the same type, use an array instead of a tuple (tuples cannot have duplicate types). Access elements with @0, @1 (shorthand for it[0], it[1]):

type add[T] = [T]2;
impl[T] add[T] -> T { @0 + @1 }

print(add[i32](3, 7));
print(add[f64](1.5, 2.5));

Output:

10
4

When calling an array-based callable like add[i32](3, 7), the individual arguments are automatically mapped to the array elements. This is how operations on same-typed values work in Ty — no operator overloading needed.

Data Construction vs. Evaluation

Name { ... } always creates data (never evaluates, even if the type has an impl). Name(...) constructs and evaluates (requires an impl block). This follows Lisp's evaluation model:

type a = i32;
type b = i32;
type add = a & b;
impl add -> i32 { a.i32 + b.i32 }

// Call: constructs and evaluates
print(add(3, 7));                // 10

// Brace construction: data only, no evaluation
let pair = add { a: 3, b: 7 };
print(pair.a.i32);               // 3

// Postfix ! evaluates a construction
print(add { a: 3, b: 7 }!);      // 10

Name { ... } = data. Name(...) = construction + evaluation. value! = evaluate a previously constructed value.

Uniform Function Call Syntax

Any function or callable tuple can be called with dot syntax. x.f(y) is sugar for f(x, y):

type n = i32;
fn twice n -> i32 { n.i32 * 2 }
fn inc n -> i32 { n.i32 + 1 }

// These are equivalent:
print(inc(twice(5)));
print(5.twice().inc());

Output:

11
11

Since Ty matches arguments by type (not position), x.f(y) and y.f(x) are both valid. UFCS enables chaining and a more readable left-to-right data flow.

No Traits

Ty has no traits. Where Rust uses impl Trait for Type, Ty uses impl blocks directly on types. The callable tuple is the interface. Polymorphism comes from type-based dispatch on tuples — if a type has an impl, it's callable.

Orphan Rules

Ty's orphan rules are more flexible than Rust's. You may write an impl block on a tuple type if at least one type in that tuple is defined in your crate. No newtype wrappers required for cross-crate impls.

Generics

Ty adds generics to T. Generic parameters are declared in square brackets:

fn identity[T] T -> T {
    it
}

print(identity(42), identity(true));

Output:

42 true

The compiler infers type arguments from the call site. identity(42) infers T = i32, identity(true) infers T = bool. Each instantiation is monomorphized into a separate concrete function.

Multiple Type Parameters

type a;
type b;
fn first[A & B] a[A] & b[B] -> A { a }
fn second[A & B] a[A] & b[B] -> B { b }

print(first[i32 & bool](10, true), second[i32 & bool](10, true));

Output:

10 true

Generic Types

Types can also be generic. Define generic field types and combine them:

type x;
type y;
type Point[T] = x[T] & y[T];

let p = Point[i32] { x: 3, y: 4 };
print(p.x, p.y);

Output:

3 4

Point[i32] monomorphizes to Point$i32 with fields x$i32 and y$i32. The field types x[T] and y[T] are also monomorphized transitively.

Const Generics

Type parameters can also be compile-time constants. Declare them with a type annotation:

type N = u64;
type Buffer[T]N = [T]N;
let b = Buffer[i32]4: [1, 2, 3, 4];
print(b[0], b[3]);

A const parameter is written as a suffix after the closing bracket, and its name must be a previously declared type — that is what tells it apart from a type parameter. Const generic parameters are substituted during monomorphization, just like type parameters. The array size N becomes a concrete value in each instantiation.

Arithmetic on const parameters works in type positions:

type N = u64;
type Pair[T]N = [T](N * 2);
let p = Pair[i32]3: [1, 2, 3, 4, 5, 6];
print(p[0], p[5]);

Library Handles

A newtype with an impl on itself owns its value, and the impl is its destructor. Ty calls it when a local goes out of scope, and it checks that an object made from another one does not outlive it. The lifetime belongs to the value, so a plain integer handle is checked as well as a pointer, and nothing is annotated:

type Device = i32;
type Buffer = i32;

fn open_device(id: i32) -> Device { Device: id }
fn create_buffer(ref device: Device, size: i32) -> Buffer { Buffer: size }
fn buffer_size(ref buffer: Buffer) -> i32 { buffer.i32 }

impl Device { print(10, it); 0 }
impl Buffer { print(20, it); 0 }

fn main -> i32 {
    let device = open_device(7);
    let buffer = create_buffer(device, 64);
    print(buffer_size(buffer));
    0
}
64
20 64
10 7

buffer depends on device because it was created from it, so it is dropped first. ref says that create_buffer only borrows the device. Destroying the device by hand with Device(device); right after let buffer is rejected, because the buffer is still used afterwards:

Borrow error: tutorial.c.ty:15:23: 'buffer' depends on 'device' (created from it on line 13), but 'device' was destroyed on line 14

The rules, including extern fn bindings, are in the reference under Library Handles.

Compilation

Ty compiles to T through two passes:

  1. Monomorphization — generic functions and types are duplicated for each concrete type they're used with. identity[T] called with i32 and bool becomes identity$i32 and identity$bool. Generic type dependencies are resolved transitively.
  2. Lowering — impl blocks become functions, callable constructions become function calls, brace constructions become T struct literals.

The resulting T code is then compiled to C by T's backend. Ty can also skip T and compile to safe Rust with --rust, where generics stay generic — see the Rust interop guide.

What Ty Adds Over T

All of these are described in the Ty reference.

What's Next

Not implemented yet: