strict-script strict-script

Start

Examples

Six small programs. All of them are compiled and run by the tests (packages/cli/test): what a comment says a function does, a test saw it do.

strict-script check examples                  # nothing is refused
strict-script run examples/report/report.sts  # calls its `main`

Getting started says how the clone gives the command strict-script.

The first four are the examples of strict-ts, written again. There each is written twice, classic and conforming; here the classic one cannot be written.

ExampleAboutIt uses
invoiceTyping and errors: what comes from outside is read against its typeType.parse, JSON.parse, ?, a match that covers every case, an extern
reportWhat throws without saying so in JavaScriptA pattern from the outside, BigInt, a bigint division, toFixed: each an Option or a Result
cartExplicit mutabilityRead-only records and copies, const mut, a parameter that says mut, a value given away
transferOwnershipA resource ended by its scope
wordcountThe file system as the outsideextern "node:fs", a Map that holds the counts
serverA web server, and a request to itFunctions handed to the outside, values of the outside, async

#report: ways to throw that no type announces

export function average(sales: Sale[]): Result<bigint, string> {
  const total = BigInt(sales.sum(sale => sale.cents)).okOr("a price is not a whole number of cents")?
  const count = BigInt(sales.length).okOr("not a count")?
  (total / count).okOr("no sale matches")
}

BigInt(1.5) throws in JavaScript, and so does a division of bigints by zero. Here both give an Option, and the function cannot be written without saying what happens then. Run on the pattern (, the report gives Err("Invalid regular expression…"); on sales that match nothing, Err("no sale matches"); on text that is not a list of sales, Err("at [0].product: expected a text").

#cart: who changes what

export function cheapestFirst(items: Item[]): Item[] {
  items.sortedBy(item => item.cents)             // a sorted copy: `items` cannot be sorted in place
}

export function restock(mut counts: Map<string, number>, item: Item) {
  counts.set(item.name, (counts.get(item.name) ?? 0) + item.quantity)
}

const mut stock: Map<string, number> = Map.from([{ key: "pen", value: 10 }])
restock(mut stock, pen)                           // the call says `stock` may change
const counted: Map<string, number> = stock       // given away: `stock` is no longer a name to use

#transfer: a transaction with one owner

export function transfer(from: string, to: string, cents: number): Result<string[], "nothing-to-move"> {
  const tx = begin()
  debit(mut tx, from, cents)
  if (cents <= 0) return Err("nothing-to-move")    // rolled back here
  credit(mut tx, to, cents)
  Ok(commit(tx))                                   // given away: any use of `tx` after this is refused
}

No line is written for the paths that fail, and none can be forgotten: rollback is named once, where the resource is declared (bank.sts), and is called when the scope of tx ends with the transaction still there.

#In the playground

Shorter examples, to change and run in a page: the playground of the website (pnpm build && pnpm site, then packages/site/dist/playground.html).

Esc

    Type a word of the language, a rule, a command: mut, extern, match-not-covered.