Guides / Modules, packages, and publication

Modules, packages, and publication

A receipt needs pricing, but another tool may need the same rule. Put the calculation in its own module and export it.

Create the files#

With Silk installed, create a fresh workspace:

mkdir order-modules
cd order-modules
mkdir src build

src/pricing.slk#

package orders::pricing;

import { expect_equal } from "std/test";

export fn order_total (unit_price: int, quantity: int, delivery: int) -> int {
  return unit_price * quantity + delivery;
}

test "shared pricing includes delivery once" {
  expect_equal(2900, order_total(1200, 2, 500));
}

src/main.slk#

package orders::receipt;

import { println } from "std/io";
import { order_total } from "./pricing.slk";

fn main () -> int {
  println("Receipt: {d} cents", order_total(1200, 2, 500));
  return 0;
}

export makes the rule available to the importer. ./pricing.slk is relative to main.slk. Source namespaces use ::; quoted file paths use /.

Build the module set#

silk check src/main.slk src/pricing.slk
silk test src/main.slk src/pricing.slk
silk build src/main.slk src/pricing.slk -o build/receipt
./build/receipt

The pricing test should pass and the receipt should print Receipt: 2900 cents.

Give the package a home#

When listing files becomes repetitive, add a manifest and use --package .. For another package on disk, add a local dependency.

Before sharing a package, define its distribution files. Keep ABI definitions separate from normal source imports.

Reference#

Import grammar · Manifest fields · Prelude globals

Source repository · Edit this page · View Markdown

Quick links

Type to search across the site. Use the up and down arrow keys to choose a result, Enter to open it, and Escape to close.