Sn Tin

Learn Tin · Part 1: The language · Chapter 13

Modules

Split a program across files with import.

Splitting a program into files

When a program grows, put related types and functions in their own file and import it with a path relative to the importing file. Here is a small file about robot joints:

lib/joints.tin
// lib/joints.tin: a file of types and functions for other files to use.

type Joint = { name: string, angle: float, limit: float }

/// The names of the joints that are past their limit.
fn overLimit(joints: Joint[]): string[] {
  joints.filter((j) => abs(j.angle) > j.limit).map((j) => j.name)
}

A second file builds on it. It uses Joint, so it imports joints.tin too:

lib/report.tin
// lib/report.tin: it uses Joint, so it imports joints.tin itself.
import "./joints.tin"

fn report(joints: Joint[]): string {
  let bad = overLimit(joints)
  if bad.isEmpty() { "no joint is over the limit" } else { "over the limit: {join(bad, ", ")}" }
}

And the program itself:

arm.tintin run arm.tin
import "./lib/joints.tin"
import "./lib/report.tin"

fn main() {
  let arm = [
    Joint { name: "shoulder", angle: 1.2, limit: 1.6 },
    Joint { name: "elbow", angle: -2.4, limit: 2.1 },
    Joint { name: "wrist", angle: 3.0, limit: 2.8 },
  ]
  print(report(arm))
  print(report([arm[0]]))
}
over the limit: elbow, wrist
no joint is over the limit

tin run arm.tin finds the other files on its own. Each file is included once, even though two files import joints.tin.

One namespace

Today, all the files of a program share one namespace, as if they were pasted into one file. There are no pub markers and no prefixes like joints.overLimit. That keeps small programs simple, but it means a name can be defined only once across all files:

twice.tintin check twice.tin
import "./lib/joints.tin"

// joints.tin already has a type called Joint
type Joint = { name: string }

fn main() {
  print(Joint { name: "elbow" })
}
error: 'Joint' is already defined
 --> lib/joints.tin:3:1
  |
3 | type Joint = { name: string, angle: float, limit: float }
  | ^^^^^^^^^^

The error points at the copy in lib/joints.tin because the main file is read first. Either way, the fix is to rename one of them.

Imports of JavaScript modules look different (import js "...") and come in Part 3.

Tasks

●○○ Task 1: A temperature module

Put toFahrenheit and toCelsius in lib/temps.tin, import it, and check that converting 21.5 there and back gives 21.5.

Solution
task-1.solution.tintin run task-1.solution.tin
import "./lib/temps.tin"

fn main() {
  print(toFahrenheit(100.0))
  print(toCelsius(212.0))
  print(toCelsius(toFahrenheit(21.5)))
}
212.0
100.0
21.5

●●○ Task 2: Fix the clash

This program imports two modules that both define label. Read the error, then make it print a price and a day.

task-2.tintin check task-2.tin
import "./lib/money.tin"
import "./lib/dates.tin"

fn main() {
  print(label(1205))
  print(label(3))
}
error: 'label' is already defined
 --> lib/dates.tin:1:1
  |
1 | fn label(day: int): string => "day {day}"
  | ^^^^^^^^
Solution
task-2.solution.tintin run task-2.solution.tin
// dates2.tin is dates.tin with its function renamed to dayLabel.
import "./lib/money.tin"
import "./lib/dates2.tin"

fn main() {
  print(label(1205))
  print(dayLabel(3))
}
$12.05
day 3

Every file shares one namespace today, so two functions with the same name clash. Rename one, for example to dayLabel. The error points at the second definition the compiler met.

What's next

Next: Tests. test blocks next to the code, run with tin test.

In the reference: Programs and files