Standard library · Basics

core

The smallest package in Salam, for when the program must not carry on.

import core
On this page
  1. Functions

The core package has exactly one function, but an important one. Sometimes a program reaches a point where carrying on makes no sense: data is corrupt, or a condition that "can never happen" has happened. In that case it is better to stop right there with a clear message than to quietly hand back a wrong answer.

For ordinary errors, such as bad input from a user, use result instead, so that the caller decides what to do. A panic is for when there is no correct way to continue.

Functions

Panic

func Panic(msg: str)
Stops the program where it stands and writes the message to standard error. It never returns, and defer statements do not run. The example below is not run here, because it deliberately kills the program; its output looks like salam panic: cannot divide by zero.
core-Panic.salam
import core

func divide(a: int, b: int): int:
    if b == 0:
        core.Panic("cannot divide by zero")
    end
    ret a / b
end

func main:
    println divide(10, 2)
    println divide(1, 0)
end