Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Tutorial: from zero to three targets

This tutorial builds a small library (a book catalog), tests it, and runs it as a TypeScript, Python, and Go package. The complete example lives in examples/library.

The normative description of every rule is the language specification. This page is the shortest path from an empty directory to three working libraries.

1. Install the compiler

curl -fsSL https://raw.githubusercontent.com/gntrees/lingua-tungga/main/scripts/install.sh | bash
lt --help

Or build from source:

cargo build --release
export PATH="$PWD/target/release:$PATH"

To run tests you also need the native toolchain:

TargetRequires
TypeScriptNode 20+
PythonPython 3.11+
GoGo 1.22+

See Install for pins and mirrors.

2. Write the model

examples/library/book.lt:

package book {
  export enum Genre {
    Fiction
    NonFiction
    Reference
  }

  export struct Book {
    title: Str
    author: Str
    genre: Genre
    year: Int
  }

  export fn genre_label(b: Book) -> Str {
    match b.genre {
      Genre.Fiction => "fiction"
      Genre.NonFiction => "non-fiction"
      Genre.Reference => "reference"
    }
  }

  export fn describe(b: Book) -> Str {
    `"${b.title}" by ${b.author} (${b.year}, ${b.genre_label()})`
  }
}

Things to note:

  • A file contains exactly one package block.
  • A struct is only data. Behavior lives in package-level functions; b.genre_label() is sugar for genre_label(b).
  • All fields are immutable. There is no null; absence uses Option<T>.
  • match must be exhaustive.
  • A plain template uses Display. Int, Str, and structs all have it.

3. Write the logic

examples/library/catalog.lt:

import book { Book } "./book"

package catalog {
  export struct Catalog {
    books: List<Book>
  }

  export fn empty() -> Catalog {
    Catalog(books: [])
  }

  export fn add(c: Catalog, book: Book) -> Catalog {
    Catalog(books: c.books.push(book))
  }

  export fn size(c: Catalog) -> Int {
    c.books.len()
  }

  export fn find_by_author(c: Catalog, author: Str) -> List<Book> {
    c.books.filter(fn(book) => book.author == author)
  }

  export fn newest(c: Catalog) -> Option<Book> {
    let mut best: Option<Book> = None
    for book in c.books {
      best = match best {
        None => Some(book)
        Some(current) => if book.year > current.year {
          Some(book)
        } else {
          best
        }
      }
    }
    best
  }
}

Language notes:

  • Imports name packages, not symbols: import book { Book } "./book" imports the package book from that file and takes Book. Without { … }, the package is bound as a qualifier (book.Book).
  • Collections are immutable: push and add return a new value.
  • A function whose parameters are not struct-typed is an ordinary function: call empty().
  • Parameter order determines the receiver: add(c, book) can be written c.add(book).
  • mut is only for local variables; struct fields are never mutable.

4. Write tests

examples/library/catalog_test.lt:

import book { Book, Genre, describe } "./book"
import catalog { Catalog, add, empty, find_by_author, newest, size } "./catalog"

package catalog_test {
  fn sample() -> Catalog {
    let first = Book(title: "Dune", author: "Frank", genre: Genre.Fiction, year: 1965)
    let second = Book(title: "Code", author: "Ada", genre: Genre.Reference, year: 2020)
    add(add(empty(), first), second)
  }

  test "describes a book" {
    let book = Book(title: "Dune", author: "Frank", genre: Genre.Fiction, year: 1965)
    expect.eq(describe(book), "\"Dune\" by Frank (1965, fiction)")
  }

  test "adds books" {
    expect.eq(size(sample()), 2)
  }

  test "filters by author" {
    expect.eq(find_by_author(sample(), "Ada").len(), 1)
    expect.eq(find_by_author(sample(), "Nobody").len(), 0)
  }

  test "newest uses year" {
    expect.eq(newest(sample()).map(fn(book) => book.title), Some("Code"))
  }

  test "empty catalog has no newest" {
    expect.eq(newest(empty()), None)
  }
}
  • Tests are package items written test "title" { … }; titles are unique per package.
  • fn sample() is a private helper because it has no export.
  • Assertions use expect.eq, expect.not_equal, expect.true, and expect.false.

5. Check and format

lt check examples/library/catalog_test.lt
lt fmt examples/library

lt fmt preserves comments. Inline comments (x: Int // note) are rejected; move them to their own line.

6. Compile and run

lt test -t ts examples/library/catalog_test.lt --out out/lib --run
lt test -t py examples/library/catalog_test.lt --out out/lib --run
lt test -t go examples/library/catalog_test.lt --out out/lib --run

--run uses the native runner: vitest, pytest, or go test. Without it, only the test files are emitted.

To build only the library:

lt build -t ts examples/library/catalog.lt --out out/lib
lt build -t py examples/library/catalog.lt --out out/lib
lt build -t go examples/library/catalog.lt --out out/lib

One package becomes one host module. The example has two packages in two files, so the output is two modules:

out/ts/src/book.ts, out/ts/src/catalog.ts
out/py/gen/book.py, out/py/gen/catalog.py
out/go/gen/book/book.go, out/go/gen/catalog/catalog.go

The TypeScript result is roughly:

export class Book {
  readonly title: string;
  readonly author: string;
  readonly genre: Genre;
  readonly year: bigint;
}

Names follow the target convention: snake_case becomes camelCase in TypeScript. Declarations without export stay private in LT; the emitters add a _ prefix (TypeScript, Python) or keep a lowercase initial (Go) so the package’s own tests can still call them.

7. Depend on a git repository

External dependencies are declared in lt.toml:

[dependencies]
foo = { git = "https://github.com/foo/bar", version = "^1.2" }

The import uses the dependency key and the package path inside that repository:

import user { User } "foo/src/user"

lt update resolves versions and writes lt.lock (commit and SHA-256). lt build and lt check never write the lock; an incomplete lock is an error. Related commands:

lt add foo --git https://github.com/foo/bar --version "^1.2"
lt update foo      # one dependency and its closure
lt update --all
lt vendor          # copy locked sources into vendor/ for offline CI

See Packages and dependencies and the import v3 design note.

8. What this language does not do

There is no IO, async, exceptions, inheritance, null, typeclass, or main. A Lingua Tungga program is a pure library; side effects (HTTP, files, UI) remain in the host language.