Standard library · Basics

core

Stopping a program with a clear message, and Option for a value that may or may not be there.

import core

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, declare the function throws and throw an Error instead, so that the caller decides what to do with try and catch. A panic is for when there is no correct way to continue, and catch does not stop it.

core also defines the built-in error types, Error and ErrorKind, which every program can use without an import. The error package explains how to throw, pass on and catch them.

Guarding an invariant

An invariant is something your own code promises is always true: a balance never drops below zero, a list stays sorted, an index stays in range. Checks for ordinary, expected failures return a value the caller can act on. A check for a broken invariant calls Panic, because if it fails the bug is in the program itself and any result computed after that point would be wrong.

core-guide-invariant.salam
import core

struct Account:
    pub owner: str = ""
    pub balance: int = 0
end

func withdraw(acc &: Account, amount: int): bool:
    if amount > acc.balance:
        ret false
    end
    acc.balance -= amount
    if acc.balance < 0:
        core.Panic("balance went negative for " + acc.owner)
    end
    ret true
end

func main:
    mut acc := Account { owner = "Sara", balance = 100 }
    println withdraw(acc, 30), acc.balance
    println withdraw(acc, 500), acc.balance
    println withdraw(acc, 70), acc.balance
end
Outputtrue 70 false 70 true 0

Asking for more money than the account holds is an ordinary situation, so withdraw answers false. The Panic after the subtraction can only run if someone later changes the function and breaks it; it is there so that mistake shows up at once, with the owner's name in the message, instead of as a strange number weeks later.

Marking code that should never run

A function that must return a value for every input still needs something at the end, after all the known cases. When the remaining inputs are impossible by design, a Panic with the bad value in the message is the honest way to finish it. A made up return value, such as an empty string, would hide the bug and let it travel further.

core-guide-unreachable.salam
import core

func next_light(light: str): str:
    switch light:
        "red":
            ret "green"
        end
        "green":
            ret "yellow"
        end
        "yellow":
            ret "red"
        end
    end
    core.Panic("unknown light: " + light)
end

func main:
    mut light := "red"
    repeat 4:
        nxt := next_light(light)
        println light, "->", nxt
        light = nxt
    end
end
Outputred -> green green -> yellow yellow -> red red -> green

Because Panic never returns, the compiler accepts it as the last statement of a function with a result type. Put the unexpected value into the message: unknown light: blue tells you far more than unreachable.

When the cases are the members of an enum, prefer match: it must cover every member, so the compiler finds the missing case for you and no Panic is needed.

Panic, or a value that reports failure?

The two work together. Anything that comes from outside the program, such as user input, files, the network or another service, can be wrong in normal operation, so make the function throws and let the caller decide with try and catch, or return a value that carries the failure, such as an enum you declare yourself with an Err case, or the ? operator on one. Once the value has been checked, inner functions may treat a bad value as a programming error and panic.

core-guide-vs-result.salam
import core

enum Result<T, E>:
    Ok(value: T)
    Err(error: E)
end

func parse_percent(text: str): Result<int, str>:
    n := text.ToInt()
    if n < 0 or n > 100:
        ret Result.Err("a percentage must be between 0 and 100, got " + text)
    end
    ret Result.Ok(n)
end

func apply_discount(price: int, percent: int): int:
    if percent < 0 or percent > 100:
        core.Panic("apply_discount called with " + percent)
    end
    ret price - price * percent / 100
end

func main:
    each typed in ["20", "250"]:
        r := parse_percent(typed)
        match r:
            Ok(p): println "price after discount:", apply_discount(80, p) end
            Err(msg): println "rejected:", msg end
        end
    end
end
Outputprice after discount: 64 rejected: a percentage must be between 0 and 100, got 250
SituationUse
The user typed something invalidthrows and throw an Error
A file or server is missingthrows, or the Error out-parameter of that package
A value may be absentOption, below, or a field written T?
Your own code broke a rule it promised to keepPanic
A case that cannot happen by designPanic, or match on an enum

What a panic does

  • It writes salam panic: and your message to standard error, then calls the C abort. The process ends with signal SIGABRT, which a shell reports as exit status 134, and the system may write a core dump.
  • defer statements do not run, so open files are not flushed and temporary files stay behind.
  • Text the program printed before the panic is written out first, even when standard output is a pipe or a file and println was still holding it in its buffer, so the panic message comes after the last printed line.
  • In a server, one bad request that panics takes down every other request too.

That is the main reason to keep Panic for real bugs only.

Option: a value that may or may not be there

Sometimes the answer from a function is "nothing": a search found no match, or a setting was never given. Returning minus one or an empty string for that is convenient, and later forgotten, and then it is a bug. Option<T> makes it explicit: either there is a value of type T, or there is not. T? is short for core.Option<T> and needs no import: a field written timeout: int? may be left out of a literal, plain values wrap on their own, and null means empty. See optional fields.

  • Option is a plain struct with two fields, value and present, not an enum: test it with is_some() or .present, not with match, and the ? try-operator does not work on it.
  • Never read .value before checking. On an empty option it is the zero value of T: 0 for numbers, but for a str it is a null pointer, and printing or measuring it crashes the program.
  • Build options only with Some and None (or plain assignment and null through T?), so the two fields always agree.
  • The free functions Some, None, IsSome, IsNone, UnwrapOr, Unwrap and Expect need import core and the core. prefix; the methods is_some(), is_none(), unwrap_or(x) and unwrap() on a value already typed T? need neither.

A lookup that may find nothing

Looking something up is the classic case for an option. Returning an empty string for a missing phone number would be ambiguous, since an empty value could also be stored on purpose. A function that returns str? says plainly in its signature that the answer may be missing, and every caller has to deal with that.

core-guide-option-lookup.salam
func phone_of(book: HashMap<str, str>, name: str): str?:
    if book.Has(name):
        ret book.Get(name)
    end
    ret null
end

func main:
    mut book := HashMap {} as HashMap<str, str>
    defer book.Free()
    book.Put("Sara", "0912 111 2233")
    book.Put("Omid", "0935 444 5566")
    each name in ["Omid", "Neda"]:
        p := phone_of(book, name)
        if p.is_some():
            println name + ":", p.value
        else:
            println name + ": no number on file"
        end
    end
    println "call", phone_of(book, "Neda").unwrap_or("the front desk")
end
OutputOmid: 0935 444 5566 Neda: no number on file call the front desk

is_some() followed by .value suits code that does two different things. When there is a sensible default, unwrap_or does the check and the fallback in one call, as the last line shows.

Optional settings with defaults

An optional field tells "not given" apart from a real zero or an empty string, which a plain int field cannot. Give each optional field null as its default, so a struct literal can leave it out, and apply the real default at the point of use.

core-guide-option-settings.salam
struct Settings:
    pub width: int? = null
    pub title: str? = null
end

func describe(s: Settings): str:
    w := s.width.unwrap_or(80)
    t := s.title.unwrap_or("untitled")
    ret t + " (" + w + " columns)"
end

func main:
    println describe(Settings {})
    println describe(Settings { width = 120 })
    println describe(Settings { width = 0, title = "report" })
end
Outputuntitled (80 columns) untitled (120 columns) report (0 columns)

The third line shows why this matters: a width of 0 was given on purpose and is kept, while the first two fall back to 80.

Searching a list of structs

A search over a vector returns the first match or nothing. Here the program knows an open task must exist at that moment, so it uses core.Expect: if the assumption is ever wrong, the program stops with that sentence rather than carrying on with an empty name.

core-guide-option-first.salam
import core

struct Task:
    pub name: str = ""
    pub done: bool = false
end

func first_open(tasks: Vector<Task>): str?:
    each t in tasks:
        if not t.done:
            ret t.name
        end
    end
    ret null
end

func main:
    mut tasks := Vector {} as Vector<Task>
    defer tasks.Free()
    tasks.Push(Task { name = "write tests", done = true })
    tasks.Push(Task { name = "fix login bug" })
    tasks.Push(Task { name = "update docs" })
    next := first_open(tasks)
    println "next up:", core.Expect(next, "there is always an open task here")
    each i, t in tasks:
        tasks.Set(i, Task { name = t.name, done = true })
    end
    println "anything left?", first_open(tasks).is_some()
end
Outputnext up: fix login bug anything left? false

core.Unwrap stops with a generic message when the option is empty. Prefer core.Expect, whose message tells the reader which assumption failed, and keep it for assumptions that would be a bug in your own program, the same rule as Panic above. When the caller needs to know why something failed rather than just that a value is missing, make the function throws instead, or declare your own enum with an Err case.

Types

ErrorKind

enum ErrorKind
The general category of an error. Code decides on the kind; the message is for people.
NameTypeDescription
OtherAny error that fits none of the other kinds. A throw "message" gets this kind.
SyntaxThe input does not follow its format, such as a missing comma in JSON.
UnexpectedEndThe input ended in the middle of a value.
TypeMismatchA value has another type than the one asked for.
MissingKeyA required key or field is not there.
OutOfRangeA number, index or size is outside the allowed range.
InvalidUtf8Text is not valid UTF-8.
InvalidArgumentThe caller passed a value the function cannot accept.
NotFoundWhat was asked for does not exist.
AlreadyExistsSomething that should be new is already there.
PermissionDeniedThe operation is not allowed.
IoA read or write failed; trying again may work.
TimeoutTime ran out; trying again may work.
CanceledThe work was stopped on purpose.
UnavailableA service is unavailable for now; trying again may work.
UnsupportedThe input or the request uses something that is not supported.
InternalA fault inside the program itself.
core-ErrorKind.salam
func retryable(e: Error): bool:
    ret e.is(ErrorKind.Io) or e.is(ErrorKind.Timeout) or e.is(ErrorKind.Unavailable)
end

func main:
    kinds := [ErrorKind.Io, ErrorKind.Timeout, ErrorKind.Unavailable, ErrorKind.NotFound, ErrorKind.PermissionDenied, ErrorKind.Internal]
    each kind in kinds:
        e := Error.New(kind, "failed")
        if retryable(e):
            println e.kind_name() + ": try again"
        else:
            println e.kind_name() + ": give up"
        end
    end
end
OutputI/O error: try again timed out: try again unavailable: try again not found: give up permission denied: give up internal error: give up

Error

struct Error
The error that throw sends and catch receives. It holds only text and numbers, so copying it is cheap and safe.
NameTypeDescription
kindErrorKindThe general category, one of ErrorKind.
codestr = ""A precise code chosen by the package that threw the error, such as json.trailing_data; empty when there is none.
messagestr = ""What went wrong, for a person to read.
contextstr = ""The explanations added by wrap, outermost first, joined with ": ".
pathstr = ""Where in the data the problem is, such as $.users[3].age, or a file path; empty when unknown.
offsetint = -1The byte offset of the problem in the input, or -1 when unknown.
lineint = 0The line of the problem, counting from 1, or 0 when unknown.
columnint = 0The column of the problem, counting from 1, or 0 when unknown.
core-Error.salam
func parse age(text: str): int throws:
    if text == "":
        throw Error.New(ErrorKind.MissingKey, "the age is missing")
    end
    n := text.ToInt()
    if n < 0 or n > 150:
        throw Error.New(ErrorKind.OutOfRange, "the age " + n + " is out of range").with_code("age.range")
    end
    ret n
end

func main:
    each text in ["42", "", "200"]:
        try:
            println "age", parse age(text)
        catch e:
            println e.kind_name() + ":", e.message
            println "  code:", e.code, "| out of range:", e.is(ErrorKind.OutOfRange)
        end
    end
    e := Error.At(ErrorKind.Syntax, "expected a digit", 4).with_path("$.age")
    println e.ToStr()
    placed := e.with_position(4, 2, 3).wrap("reading the form")
    println placed.ToStr()
    println placed.offset, placed.line, placed.column, placed.context
end
Outputage 42 missing key: the age is missing code: | out of range: false out of range: the age 200 is out of range code: age.range | out of range: true expected a digit at $.age (offset 4) reading the form: expected a digit at $.age (line 2, column 3) 4 2 3 reading the form

Is

func Is(kind: ErrorKind): bool

is

func is(kind: ErrorKind): bool
True when the error is of this kind.
core-Error.is.salam
func read note(name: str): str throws:
    if name == "":
        throw Error.New(ErrorKind.InvalidArgument, "the name is empty")
    end
    if name != "notes.txt":
        throw Error.New(ErrorKind.NotFound, name + " does not exist")
    end
    ret "contents of " + name
end

func main:
    each name in ["notes.txt", "todo.txt", ""]:
        try:
            println read note(name)
        catch e:
            if e.is(ErrorKind.NotFound):
                println "create it first:", e.message
            else:
                println "cannot go on:", e.message
            end
        end
    end
end
Outputcontents of notes.txt create it first: todo.txt does not exist cannot go on: the name is empty

Wrap

func Wrap(message: str): Error

wrap

func wrap(message: str): Error
A copy with an explanation added in front of the context. The kind, code and position stay.
core-Error.wrap.salam
func read port(text: str): int throws:
    n := text.ToInt()
    if n < 1 or n > 65535:
        throw Error.New(ErrorKind.OutOfRange, "port " + n + " is not valid")
    end
    ret n
end

func load config(text: str): int throws:
    try:
        ret read port(text)
    catch e:
        throw e.wrap("reading the port")
    end
end

func main:
    try:
        println load config("80")
        println load config("70000")
    catch e:
        println e.ToStr()
        println "context:", e.context
        println "kind:", e.kind_name()
    end
end
Output80 reading the port: port 70000 is not valid context: reading the port kind: out of range

WithCode

func WithCode(code: str): Error

with_code

func with_code(code: str): Error
A copy with the code set.
core-Error.with_code.salam
func parse age(text: str): int throws:
    n := text.ToInt()
    if n < 0 or n > 150:
        throw Error.New(ErrorKind.OutOfRange, "the age " + n + " is out of range").with_code("age.range")
    end
    ret n
end

func main:
    each text in ["30", "200"]:
        try:
            println "age", parse age(text)
        catch e:
            if e.code == "age.range":
                println "ask again:", e.message
            end
            println "code:", e.code
        end
    end
end
Outputage 30 ask again: the age 200 is out of range code: age.range

WithPath

func WithPath(path: str): Error

with_path

func with_path(path: str): Error
A copy with the path set.
core-Error.with_path.salam
func check(user: str, age: int): int throws:
    if age < 18:
        throw Error.New(ErrorKind.OutOfRange, "must be at least 18").with_path("$.users[" + user + "].age")
    end
    ret age
end

func main:
    try:
        println check("sara", 31)
        println check("omid", 12)
    catch e:
        println e.ToStr()
        println "where:", e.path
    end
end
Output31 must be at least 18 at $.users[omid].age where: $.users[omid].age

WithPosition

func WithPosition(offset: int, line: int, column: int): Error

with_position

func with_position(offset: int, line: int, column: int): Error
A copy with the offset, line and column set.
core-Error.with_position.salam
func main:
    text := "name = Sara\nage = ?\n"
    offset := text.Find("?")
    line start := text.Find("age")
    column := offset - line start + 1
    e := Error.New(ErrorKind.Syntax, "a number was expected").with_position(offset, 2, column)
    println e.ToStr()
    println e.offset, e.line, e.column
end
Outputa number was expected (line 2, column 7) 18 2 7

Locate

func Locate(text: str): Error

locate

func locate(text: str): Error
A copy with line and column worked out from offset in the given text, the input the error was found in. Lines and columns count from 1 and a column counts characters, not bytes. An error with no offset comes back unchanged.
core-Error.locate.salam
func main:
    text := "name = shop\nport = eighty\n"
    e := Error.At(ErrorKind.Syntax, "expected a number", 19)
    println e.line, e.column
    found := e.locate(text)
    println found.line, found.column
    println found.ToStr()
end
Output0 0 2 8 expected a number (line 2, column 8)

KindName

func KindName(): str

kind_name

func kind_name(): str
A short English name for the kind, such as "out of range".
core-Error.kind_name.salam
func main:
    each kind in [ErrorKind.NotFound, ErrorKind.OutOfRange, ErrorKind.Timeout, ErrorKind.PermissionDenied, ErrorKind.Other]:
        e := Error.New(kind, "details")
        println e.kind_name()
    end
end
Outputnot found out of range timed out permission denied error

ToStr

func ToStr(): str

to_str

func to_str(): str
The error as one line: context, message, path and position. println uses it too.
core-Error.to_str.salam
func main:
    plain := Error.New(ErrorKind.Timeout, "the server did not answer")
    println plain.ToStr()
    bare := Error.New(ErrorKind.NotFound, "")
    println bare.ToStr()
    located := Error.New(ErrorKind.Syntax, "a comma is missing").with_path("$.items[2]").with_position(40, 3, 9)
    println located.ToStr()
    wrapped := located.wrap("reading the order").wrap("handling the request")
    println wrapped.ToStr()
end
Outputthe server did not answer not found a comma is missing at $.items[2] (line 3, column 9) handling the request: reading the order: a comma is missing at $.items[2] (line 3, column 9)

New

static func New(kind: ErrorKind, message: str): Error
Makes an error from a kind and a message. This is the usual way to build the error you throw.
core-Error.New.salam
func find user(id: int): str throws:
    if id != 1:
        throw Error.New(ErrorKind.NotFound, "no user with id " + id)
    end
    ret "Sara"
end

func main:
    each id in [1, 7]:
        try:
            println "found", find user(id)
        catch e:
            println e.kind_name() + ":", e.message
        end
    end
end
Outputfound Sara not found: no user with id 7

At

static func At(kind: ErrorKind, message: str, offset: int): Error
Like New, and also records the byte offset in the input where the problem is.
core-Error.At.salam
func digit(text: str, at: int): int throws:
    c := text.Substr(at, 1)
    if c < "0" or c > "9":
        throw Error.At(ErrorKind.Syntax, "a digit was expected", at)
    end
    ret c.ToInt()
end

func main:
    text := "12x4"
    repeat text.Len() in i:
        try:
            println "digit", digit(text, i)
        catch e:
            println e.ToStr()
            println "offset", e.offset
        end
    end
end
Outputdigit 1 digit 2 a digit was expected (offset 2) offset 2 digit 4

Throws

type Throws = Variant<auto<T>, auto<T>>
What a throws function returns under the hood. You never write it: try and catch take it apart for you.

Tuple2

struct Tuple2<A, B>
The type of the tuple (A, B): two values kept together. You never write the name: (int, str) is this struct, ret a, b builds one and x, y := f() takes it apart.
NameTypeDescription
f0AThe first element, also written t.0.
f1BThe second element, also written t.1.

Tuple3

struct Tuple3<A, B, C>
The type of the tuple (A, B, C): three values kept together. You never write the name: (int, str) is this struct, ret a, b builds one and x, y := f() takes it apart.
NameTypeDescription
f0AThe first element, also written t.0.
f1BThe second element, also written t.1.
f2CThe third element, also written t.2.

Tuple4

struct Tuple4<A, B, C, D>
The type of the tuple (A, B, C, D): four values kept together. You never write the name: (int, str) is this struct, ret a, b builds one and x, y := f() takes it apart.
NameTypeDescription
f0AThe first element, also written t.0.
f1BThe second element, also written t.1.
f2CThe third element, also written t.2.
f3DThe fourth element, also written t.3.

Tuple5

struct Tuple5<A, B, C, D, E>
The type of the tuple (A, B, C, D, E): five values kept together. You never write the name: (int, str) is this struct, ret a, b builds one and x, y := f() takes it apart.
NameTypeDescription
f0AThe first element, also written t.0.
f1BThe second element, also written t.1.
f2CThe third element, also written t.2.
f3DThe fourth element, also written t.3.
f4EThe fifth element, also written t.4.

Tuple6

struct Tuple6<A, B, C, D, E, F>
The type of the tuple (A, B, C, D, E, F): six values kept together. You never write the name: (int, str) is this struct, ret a, b builds one and x, y := f() takes it apart.
NameTypeDescription
f0AThe first element, also written t.0.
f1BThe second element, also written t.1.
f2CThe third element, also written t.2.
f3DThe fourth element, also written t.3.
f4EThe fifth element, also written t.4.
f5FThe sixth element, also written t.5.

Tuple7

struct Tuple7<A, B, C, D, E, F, G>
The type of the tuple (A, B, C, D, E, F, G): seven values kept together. You never write the name: (int, str) is this struct, ret a, b builds one and x, y := f() takes it apart.
NameTypeDescription
f0AThe first element, also written t.0.
f1BThe second element, also written t.1.
f2CThe third element, also written t.2.
f3DThe fourth element, also written t.3.
f4EThe fifth element, also written t.4.
f5FThe sixth element, also written t.5.
f6GThe seventh element, also written t.6.

Tuple8

struct Tuple8<A, B, C, D, E, F, G, H>
The type of the tuple (A, B, C, D, E, F, G, H): eight values kept together. You never write the name: (int, str) is this struct, ret a, b builds one and x, y := f() takes it apart.
NameTypeDescription
f0AThe first element, also written t.0.
f1BThe second element, also written t.1.
f2CThe third element, also written t.2.
f3DThe fourth element, also written t.3.
f4EThe fifth element, also written t.4.
f5FThe sixth element, also written t.5.
f6GThe seventh element, also written t.6.
f7HThe eighth element, also written t.7.

Option

struct Option<T>
A struct that keeps a value and a "present or not" flag side by side. Build one with Some or None, so that the two fields always agree. Usually written T?.
NameTypeDescription
valueTThe value itself. When present is false this is the zero value of T; for a str that is a null pointer that crashes when used, so check present first.
presentboolTrue when there is a value.
core-Option.salam
import core

func search(names: Vector<str>, wanted: str): core.Option<int>:
    each i, name in names:
        if name == wanted:
            ret core.Some(i)
        end
    end
    ret core.None()
end

func main:
    mut names := Vector {} as Vector<str>
    defer names.Free()
    names.Push("Ali")
    names.Push("Maryam")
    found := search(names, "Maryam")
    missing := search(names, "Reza")
    println core.IsSome(found), core.IsNone(found), found.value, found.present
    println core.IsSome(missing), core.IsNone(missing)
    println core.Unwrap(found), core.Expect(found, "this should have been found")
    println core.UnwrapOr(missing, -1)
end
Outputtrue false 1 true false true 1 1 -1

IsSome

func IsSome(): bool

is_some

func is_some(): bool
The method form of IsSome: true when there is a value.
core-Methods.salam
struct Client:
    pub timeout: int?
end

func main:
    a := Client { }
    b := Client { timeout = 30 }
    println a.timeout.is_some(), a.timeout.is_none(), a.timeout.unwrap_or(5)
    println b.timeout.is_some(), b.timeout.is_none(), b.timeout.unwrap_or(5), b.timeout.unwrap()
end
Outputfalse true 5 true false 30 30

IsNone

func IsNone(): bool

is_none

func is_none(): bool
The method form of IsNone: true when it is empty.

The example for this item is shown with the ones above: core-Methods.salam

UnwrapOr

func UnwrapOr(fallback: T): T

unwrap_or

func unwrap_or(fallback: T): T
The method form of UnwrapOr: the value, or the fallback you supply when it is empty.

The example for this item is shown with the ones above: core-Methods.salam

Unwrap

func Unwrap(): T

unwrap

func unwrap(): T
The method form of Unwrap: the value, or a panic when it is empty.

The example for this item is shown with the ones above: core-Methods.salam

Functions

Panic

func Panic(msg: str)
Stops the program where it stands: writes salam panic: and the message to standard error and aborts the process (exit status 134). It never returns and defer statements do not run; standard output the program printed before it is written out first. 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

Some

func Some<T>(x: T): Option<T>
Builds an option that holds a value.

The example for this item is shown with the ones above: core-Option.salam

None

func None<T>(): Option<T>
Builds an empty option. The type T comes from where the value is used, such as the return type of the function.

The example for this item is shown with the ones above: core-Option.salam

IsSome

func IsSome<T>(o: Option<T>): bool
Returns true when the option holds a value.

The example for this item is shown with the ones above: core-Option.salam

IsNone

func IsNone<T>(o: Option<T>): bool
Returns true when the option is empty.

The example for this item is shown with the ones above: core-Option.salam

UnwrapOr

func UnwrapOr<T>(o: Option<T>, fallback: T): T
Returns the value, or the default you supplied when the option is empty.

The example for this item is shown with the ones above: core-Option.salam

Expect

func Expect<T>(o: Option<T>, msg: str): T
Like Unwrap, but stops with a message of your own when the option is empty; a good message makes the problem easy to find.

The example for this item is shown with the ones above: core-Option.salam

Unwrap

func Unwrap<T>(o: Option<T>): T
Returns the value. If the option is empty, the program stops with the panic called Unwrap on a None value.

The example for this item is shown with the ones above: core-Option.salam