<!-- Modules · Tin · https://tin.ad/docs/learn/modules/ · generated from the built page -->

[Learn](https://tin.ad/docs/learn/) · The language · 13 of 34

# Modules

**Goal.** 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*

```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*

```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.tin · tin run arm.tin*

```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]]))
}
```

<!-- output -->
```text
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.tin · tin check twice.tin*

```tin
import "./lib/joints.tin"

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

fn main() {
  print(Joint { name: "elbow" })
}
```

<!-- compiler output -->
```text
error: 'Joint' is already defined
 --> lib/joints.tin:3:1
  |
3 | type Joint = { name: string, angle: float, limit: float }
  | ^^^^^^^^^^
  = note: the other one is at twice.tin:4
  = note: rename one, or import one of the files as a name (`import "./x.tin" as x`) so both can stay
```

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.tin · tin run task-1.solution.tin*

```tin
import "./lib/temps.tin"

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

<!-- output -->
```text
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.tin · tin check task-2.tin*

```tin
import "./lib/money.tin"
import "./lib/dates.tin"

fn main() {
  print(label(1205))
  print(label(3))
}
```

<!-- compiler output -->
```text
error: 'label' is already defined
 --> lib/dates.tin:1:1
  |
1 | fn label(day: int): string => "day {day}"
  | ^^^^^^^^
  = note: the other one is at lib/money.tin:1
  = note: rename one, or import one of the files as a name (`import "./x.tin" as x`) so both can stay
```

**Solution

*task-2.solution.tin · tin run task-2.solution.tin*

```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))
}
```

<!-- output -->
```text
$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.

## Recap

- `import "file"` brings another file's declarations in.
- Every file shares one namespace today.
- Two functions with the same name clash: rename one.

In the reference: [Programs and files](https://tin.ad/docs/ref/language/#programs)
