Skip to main content
View

Modules & Imports

This page covers how to organize code into modules and import symbols in TypR.

Defining a module​

module Math {
    let pi <- 3.14159;
    @pub let pi_approx <- 3.14;
    @pub opaque Radians <- num;
};

A module compiles to an R environment. Members without @pub remain invisible from outside (except in build --test mode via @testable, where they are exposed as M$.test_name).

You can see this boundary directly in the playground's block graph view: only pi_approx and Radians show up as the module's outputs, while pi stays visible only once you step inside.

module Math {
    let pi <- 3.14159;
    @pub let pi_approx <- 3.14;
    @pub opaque Radians <- num;
};

Importing from modules​

# --- setup ---
module Math {
    @pub let pi_approx <- 3.14;
    @pub let sin <- fn(x: num): num { x };
};

module Stats {
    @pub let mean_of <- fn(x: num): num { x };
};
# ---------------

use Math::pi_approx;              # import a single member
use Math::{sin as s};             # import several, with an alias
use Stats::*;                     # import all @pub members

print(pi_approx);

Module-level imports​

import Math;                   # import the module itself
import Math as M;              # with an alias

Legacy forms​

mod Utils;                     # historical import form (equivalent to import)
library(dplyr);               # classic R dependency
use("dplyr", c("filter", "select"));   # legacy adapter

Module organization​

A common pattern is to use main.ty as an aggregation entry point and create one file per type or concept:

# main.ty
mod person;
mod utils;

TypR will look for person.ty and utils.ty, parse, type-check, and transpile each into a corresponding .R file in the R/ folder. This keeps the codebase clean and modular.