Skip to content
Draft

Modules

Modules organize code into reusable, namespaced units.

Each file is a module. The module name matches the file name:

src/
├── main.draft
├── utils.draft
└── models/
├── user.draft
└── post.draft

Use pub to make items public:

@// utils.draft
pub action greet(@name: String)
write("Hello, " + @name)
fin action.
pub const MAX_RETRIES = 3
pub record Config
host: String
port: Int
fin record.
import { greet, MAX_RETRIES } from utils
action main()
@greet("World")
write(string(MAX_RETRIES))
fin action.
import * from utils
action main()
@greet("World")
fin action.
import { greet as sayHello } from utils
action main()
@sayHello("World")
fin action.
import utils
action main()
@utils#greet("World")
fin action.
import { User } from ./models/user
import { Post } from ./models/post
import { Config } from ../config
import io
import fs
import math
import strings
import collections
import json
import http
import time
import process
@// math_utils.draft
pub action add(@a: Int, @b: Int) -> Int
return @a + @b
fin action.
pub action subtract(@a: Int, @b: Int) -> Int
return @a - @b
fin action.
priv action internal_helper()
@// Not exported
fin action.

For larger modules, use a directory with a mod.draft file:

math/
├── mod.draft
├── basic.draft
└── advanced.draft
@// math/mod.draft
pub import { add, subtract } from ./basic
pub import { integrate, differentiate } from ./advanced
@// api.draft
pub import { User, Post } from ./models
pub import { create, find, update } from ./actions
import { log } from std::debug when DEBUG

Define a module inline:

module utils
action greet()
write("Hello")
fin action.
fin module.
@utils#greet()
Visibility Syntax Access
Public pub Any module
Private (default) Same module
Super super Parent module
Crate crate Same crate
[package]
name = "my-app"
version = "0.1.0"
edition = "2024"
[dependencies]
http = "1.2.0"
json = { version = "1.0", features = ["serde"] }
Terminal window
draft deps install
Module Import
io import io
fs import fs
math import math
strings import strings
collections import collections
json import json
http import http
time import time
process import process

Learn about Errors for error handling.