Globals
Always Available
No import or qualification needed.
| Name | Signature | Description |
|---|---|---|
print | (a) -> () where a: Display | Print a value without trailing newline |
println | (a) -> () where a: Display | Print a value with trailing newline |
panic | (a) -> Never where a: Display | Crash with an error message |
Ok | (a) -> Result(a, e) | Construct a success Result |
Err | (e) -> Result(a, e) | Construct an error Result |
Some | (a) -> Option(a) | Construct a present Option |
None | Option(a) | The absent Option value (not a function) |
Stop | (a) -> Step(a) | End a fold_until with this value |
Continue | (a) -> Step(a) | Go on with a fold_until, with this state |
A type written as a value is its type descriptor, for a type a
parameter (json.parse(text, Int), a function of your own) and for
a trait method that makes a value (Int.empty()). The prelude’s types
need no import:
| Name | Description |
|---|---|
Int, Float, String, Bool | The primitive types |
List, Map, Set, Channel | The container types, of any element types |
Option, Result, Step | The prelude’s enums |
Bytes | Byte strings |
A type of a builtin module is a value through its module, as its other
members are (time.Weekday, task.RuntimeError, json.Value), and a type of
the program by its name.
Variants of builtin modules
The enums of a builtin module are members of the module like its functions: reach a variant through the module, or import it by name.
import channel
import channel.{ Message }
fn main() {
let ch = channel.new(1)
channel.send(ch, 1)
match channel.try_receive(ch) {
Message(v) -> println(v)
channel.Empty -> println("no value yet")
channel.Closed -> println("closed")
}
}open in playground| Name | Signature | Description |
|---|---|---|
channel.Message | (a) -> channel.ChannelResult(a) | Wraps a received channel value |
channel.Closed | channel.ChannelResult(a) | Channel is closed |
channel.Empty | channel.ChannelResult(a) | Channel buffer empty (non-blocking receive) |
channel.Received | (Channel(a), Option(a)) -> channel.Selected(a) | What a completed channel.select receive arm gives |
channel.Sent | (Channel(a)) -> channel.Selected(a) | What a completed channel.select send arm gives |
channel.Recv | (Channel(a)) -> channel.ChannelOp(a) | Build a receive arm for channel.select |
channel.Send | (Channel(a), a) -> channel.ChannelOp(a) | Build a send arm for channel.select |
time.Monday..time.Sunday | time.Weekday | Day-of-week constructors |
http.GET, http.POST, … | http.Method | HTTP method constructors |
The error enums of the builtin modules (io.IoError, json.JsonError,
…) and their variants (io.NotFound(path), …) are reached the
same way.
print
print(value: a) -> () where a: Display
Prints a value to stdout. Does not append a newline. Accepts a single value that
implements Display.
fn main() {
print("hello ")
print("world")
-- output: hello world
}open in playgroundprintln
println(value: a) -> () where a: Display
Prints a value to stdout followed by a newline. Accepts a single value that
implements Display.
A value of any length is printed, all of it. A text longer than 1,000,000
bytes is written out in pieces as it is made, so it takes no more memory than
a few pieces; it still reaches stdout whole, with no other print’s text in it.
Such a print waits for the reader as any print does: a task.deadline around
it, or another stop of its task, ends it between two pieces, and what was written
until then stays written. (In the playground a run keeps at most 1,000,000
bytes of output: a print past them is a runtime error. A host program’s own
output gets each print as one write, of at most 10,000,000 bytes.)
fn main() {
println("hello, world")
-- output: hello, world\n
}open in playgroundpanic
panic(message: a) -> Never where a: Display
Terminates execution with an error message. Accepts any value that implements
Display. A message longer than 100,000 bytes is given cut there, ending with
… (N more bytes). It never returns: its result is of type Never, the type of an expression
that does not return (return, a call of a function that never returns),
which goes with every type, so
panic can stand wherever a value is expected and ends an else block that
must leave. A program writes Never only as the return type of a
function that never returns either (see the Never
type).
-- noexec
fn main() {
panic("something went wrong")
panic(42) -- also valid
}open in playgroundOk
Ok(value: a) -> Result(a, e)
Constructs a success variant of Result.
fn main() {
let _r = Ok(42)
-- _r is Result(Int, e)
}open in playgroundErr
Err(error: e) -> Result(a, e)
Constructs an error variant of Result.
fn main() {
let _r = Err("not found")
-- _r is Result(a, String)
}open in playgroundSome
Some(value: a) -> Option(a)
Constructs a present variant of Option.
fn main() {
let x = Some(42)
match x {
Some(n) -> println(n)
None -> println("nothing")
}
}open in playgroundNone
None : Option(a)
The absent variant of Option. This is a value, not a function.
import option
fn main() {
let x = None
println(option.is_none(x)) -- true
}open in playgroundStop
Stop(value: a) -> Step(a)
The step that ends a fold that can end early (list.fold_until,
channel.fold_until, stream.fold_until): the value is what the fold
gives.
import list
fn main() {
let capped_sum = list.fold_until([1, 2, 3, 4, 5], 0) { acc, x ->
match {
acc + x > 6 -> Stop(acc)
_ -> Continue(acc + x)
}
}
println(capped_sum) -- 6
}open in playgroundA program’s own variant named Stop (a message that ends an actor)
hides this one in its module; write the prelude’s as Step.Stop(value)
there.
Continue
Continue(value: a) -> Step(a)
The step that goes on with a fold that can end early: the value is the state the next element is folded into.
channel.Message
channel.Message(value: a) -> channel.ChannelResult(a)
A variant of channel: write channel.Message(v), or
import channel.{ Message }. Wraps a value received from a channel. Returned by
channel.try_receive and channel.receive_timeout when a value is available
(channel.receive gives Some(value)).
import channel
fn main() {
let ch = channel.new(1)
channel.send(ch, 42)
when let channel.Message(v) = channel.try_receive(ch) else {
return
}
println(v) -- 42
}open in playgroundchannel.Closed
channel.Closed : channel.ChannelResult(a)
A variant of channel: write channel.Closed.
Indicates the channel has been closed and is empty. Returned by
channel.try_receive and channel.receive_timeout when no more messages will
arrive (channel.receive gives None).
channel.Empty
channel.Empty : channel.ChannelResult(a)
A variant of channel: write channel.Empty.
Indicates the channel buffer is currently empty but not closed. Returned by
channel.try_receive, and by channel.receive_timeout when nothing came in
time.
channel.Received
channel.Received(ch: Channel(a), value: Option(a)) -> channel.Selected(a)
A variant of channel: write channel.Received(ch, v). What
channel.select gives when an arm built with channel.Recv(ch) completes:
channel.Received(ch, Some(value)) for a value, channel.Received(ch, None)
when ch is closed and empty. See channel.select in
channel / task.
channel.Sent
channel.Sent(ch: Channel(a)) -> channel.Selected(a)
A variant of channel: write channel.Sent(ch). What channel.select gives
when an arm built with channel.Send(ch, value) completes: the value was
handed off. See channel.select in channel / task for
the mixed send/receive form.
channel.Recv
channel.Recv(ch: Channel(a)) -> channel.ChannelOp(a)
A variant of channel: write channel.Recv(ch). Builds a receive arm for
channel.select.
channel.Send
channel.Send(ch: Channel(a), value: a) -> channel.ChannelOp(a)
A variant of channel: write channel.Send(ch, value). Builds a send arm
for channel.select.