Skip to content

Connecting Files: The Module System

Real-world projects are often split into multiple files for better organization and maintainability. MON’s robust module system allows you to connect these files, sharing types and data in a clear and explicit way, making it ideal for multi-file configurations. Unlike JSON, which lacks a native module system, or TOML, MON provides a streamlined approach to building scalable and well-structured projects.

All import statements must appear at the top of the file, before the opening { of the root object.

In MON, the module system is simple: every key-value pair in a file’s root object is automatically available to be imported by other files. You don’t need a special export keyword.

Importing an Entire File (import * as ...)

Section titled “Importing an Entire File (import * as ...)”

This is the safest and most common way to import. It bundles all the members of another file into a single, namespaced object.

Example:

Let’s say you have a schemas.mon file defining your types:

schemas.mon

{
User: #struct {
name(String),
},
Status: #enum {
Active,
Inactive,
},
}

Now, in your main.mon file, you can import it:

main.mon

// Import everything from schemas.mon into an object named `schemas`
import * as schemas from "./schemas.mon"
{
// Now you can access the imported types via the namespace
admin_user :: schemas.User = {
name: "Admin",
},
current_status: $schemas.Status.Active,
}

Importing Specific Members (import { ... })

Section titled “Importing Specific Members (import { ... })”

You can also import specific members from another file directly into your current file’s scope. This is useful for commonly used types.

Example:

main.mon

// Import only the User struct and Status enum directly
import { User, Status } from "./schemas.mon"
{
// Now you can use them without the `schemas.` prefix
guest_user :: User = {
name: "Guest",
},
current_status: $Status.Inactive,
}

Congratulations! You now know how to structure a multi-file MON project. Next, we’ll look at some common mistakes and how to fix them.