@std/core.ms#
Error, Optional, Result, Equal, Comparable, and Hashable are prelude declarations; explicit imports of them are rejected.
Import other names with import Name from "@std/core.ms".
Clonable Interface#
A type whose values can be duplicated. Plain data is Clonable; resource handles,
natives without a clone of their own (a Regex, a Pipe), function values, and
any record, alternative or collection holding one of those are not. There is nothing
to implement: the bound T Clonable makes value.clone() available on a type
parameter.
interface Clonable {
fn clone() Self
}Clonable.clone#
Returns an independent deep copy of this value.
fn clone() SelfCloseable Interface#
A resource that can be closed. A binding that holds one is closed when its scope ends, on every exit.
The implicit close behaves like value.close()?: a failed close becomes the function’s error, after the
other bindings of the scopes it leaves have closed. A failed close is a fault where the function cannot
return an Error, or where the resource sits inside a holder (a record, list or temporary).
If the scope is already returning an error, that error is kept and the close error is dropped.
Call close() yourself to handle the failure; the binding is then consumed.
interface Closeable {
move fn close() Result[Void, Error]
}Closeable.close#
Releases the resource. The compiler calls this on every scope exit, including
returns, expected errors, faults and cancellation. A type whose release cannot fail
returns ok().
move fn close() Result[Void, Error]Comparable Interface#
A type with a total order; compare agrees with equals when both are implemented.
interface Comparable {
fn compare(other Self) Int
}Comparable.compare#
Orders this value against another of the same type: negative when it sorts before, zero when neither sorts first, positive when it sorts after.
fn compare(other Self) IntData Interface#
A type whose values are plain data: no resource, function, interface value or runtime handle
anywhere inside. Bytes cross a boundary only as data, so json.parse, json.stringify and the
typed JSON stream codecs require T Data. Promises and pipes do not require this bound.
There is nothing to implement. Records and alternatives are Data when every field is.
Among the collections, a List is Data when its element is, and a Map[String, T] is Data when T is (it is a JSON object).
A Set, a Map with another key type, a Closeable type and an opaque type are never Data.
JsonValue is Data by declaration, so a dynamic JSON object travels as a JsonValue.
interface Data {
}Equal Interface#
A type that can be compared for equality. Built-in data keeps its value semantics; an explicit implementation chooses what counts as the same value.
interface Equal {
fn equals(other Self) Bool
}Equal.equals#
Compares this value with another value of the same type.
fn equals(other Self) BoolError Type#
An expected failure as a plain value: a message, a short code callers can branch on,
and string details. Build one like any record:
Error("no such file", kind: "ENOENT", details: {"path": path}). T! is
Result[T, Error].
type Error {
message String
kind String? = null
details Map[String, String] = Map[String, String]()
}Error.details#
Extra facts about the failure, such as the path or operation, keyed by name.
details Map[String, String] = Map[String, String]()Error.kind#
A short code for what went wrong, or null when no code fits. The kind itself
never allocates when it is a literal or at most 15 bytes. The standard library
reports POSIX errno names (EAUTH is Manuscript’s own), so
error.kind == "ENOENT" means the same on every platform:
EINVAL: caller input is invalidEBADMSG: input data is malformedEOVERFLOW: a limit was exceededERANGE: a value or index is out of rangeENOENT: not foundEEXIST: already existsENOTEMPTY: directory not emptyEACCES: permission deniedENOTSUP: unsupportedENOTTY: not a terminalEBADF: resource closedEIO: a task, process or peer ended abnormallyEAGAIN: service unavailableEBUSY: resource busyENAMETOOLONG: a file name or path is too longELOOP: too many symbolic linksENOSPC: no space left on the deviceEROFS: the file system is read-onlyEXDEV: the paths are on different devicesEMFILE: too many open filesETIMEDOUT: a deadline expiredECANCELED: cancelledEAUTH: authentication failed
Your own code uses its own prefix, such as "EQUOTA".
kind String? = nullError.message#
Human-readable description of the failure.
message StringHashable Interface#
A type with a stable hash that is consistent with its Equal implementation.
interface Hashable {
fn hash() Int
}Hashable.hash#
Returns a stable hash consistent with equals for the same value.
fn hash() IntIterator Interface#
A source of items that for walks: each next returns the following item, or null once exhausted.
for item in source: calls next until null. A variable or field path advances in place and
keeps its final state, while any other expression is held by the loop alone.
next should keep returning null after the end.
The item is a borrow: it stays valid, and the iterator stays unchanged, until the loop body ends. Clone it to keep it.
interface Iterator[T] {
var fn next() T?
}Iterator.next#
Returns the next item, or null when no items remain. The item borrows from the iterator (an iterator over a container borrows from the container instead).
var fn next() T?Optional Enum#
Represents a value that may be absent: a T or null; a plain T converts where a
T? is expected.
enum Optional[T] {
some(value T)
null
}Optional.null#
An absent value with no payload; no T needs to be constructed.
nullOptional.some#
A present payload, retained by reference when T is mutable.
some(value T)Optional.some.value#
The contained value; unwrap and matching return this original payload.
value TOptional.clone#
An Optional copies its payload; null stays null.
// Receiver: T?; T Clonable
fn clone() T?Result Enum#
Holds either a successful value (ok) or an expected failure (error).
enum Result[T, E] {
ok(value T)
error(cause E)
}Result.error#
An expected failure value; it is distinct from faults and cancellation.
error(cause E)Result.ok#
A successful result, retaining its payload without a deep copy.
ok(value T)Result.error.cause#
The original domain error, retained through compatible Result propagation.
cause EResult.ok.value#
The successful value returned by unwrap, matching, or propagation.
value TResult.and_then#
Calls transform only on success and returns the Result it produces; an error
passes through. The error type stays the same.
move fn and_then[U](transform fn(move T) Result[U, E]) Result[U, E]Result.err#
Returns the error payload; check is_error() first, because a successful result
raises a fault.
fn err() EResult.get#
Returns the successful payload; an error raises a fault, so use it only when
failure is a bug. Prefer ?, if value := result: or get_or(default).
fn get() TResult.get_or#
Returns the successful payload, or default when this is an error. The default is
always evaluated; use get_or_else when it costs work.
move fn get_or(move default T) TResult.get_or_else#
Returns the successful payload; for an error, calls create once with that error
and returns its value.
move fn get_or_else(create fn(move E) T) TResult.is_error#
Reports whether this result contains an expected error.
fn is_error() BoolResult.is_ok#
Reports whether this result contains a successful value.
fn is_ok() BoolResult.map#
Transforms only a successful payload, preserving an error unchanged.
move fn map[U](transform fn(move T) U) Result[U, E]Result.map_error#
Transforms only an error, preserving a successful payload unchanged.
move fn map_error[F](transform fn(move E) F) Result[T, F]Result.or_else#
Returns this result when it is successful; for an error, calls create once with
that error and returns the Result it produces.
move fn or_else(create fn(move E) Result[T, E]) Result[T, E]Result.to_optional#
Returns some with the original success payload, or null for an error.
move fn to_optional() T?Stringable Interface#
A type with its own text form. f"{value}", print and a container’s text use
to_string when a record or alternative has one; the operation must return a
String. Every standard type with a text form meets this interface the same way, by
declaring to_string.
interface Stringable {
fn to_string() String
}Stringable.to_string#
Returns this value as text.
fn to_string() StringWritable Interface#
Data that a write accepts as either text or raw bytes. String and Bytes both implement it, so
write_file(path, "text") and write_file(path, bytes) call the same function.
Text is written as its UTF-8 encoding.
interface Writable {
fn as_bytes() Bytes
}Writable.as_bytes#
The bytes to write, as a new value the writer may keep.
fn as_bytes() Bytesjoin_errors Function#
Combines errors into one error whose message holds theirs, one per line, and whose
count detail is how many there were. Returns null when the list is empty.
fn join_errors(errors List[Error]) Error? { ... }ok_or Function#
Returns ok with the value, or error with cause when value is null. The
cause is built before the call whether or not it is used.
fn ok_or[T, E](move value T? = null, move cause E) Result[T, E] { ... }panic Function#
Raises a programming fault with the supplied message and stops the current operation.
fn panic(message String) Never { ... }print Function#
Writes value as text, followed by end (a newline unless given), to standard output. print(chunk, end: "") streams text without a newline.
A type that declares its own to_string() prints that text, also when nested in a list or map; other records print structurally.
print returns Void. Output to a terminal is flushed at each newline. Output to a pipe or file is flushed when the program blocks or exits, so a line printed before a wait is visible while it waits.
Use stdout() from @std/io/io.ms to write text with no appended newline, or stdout_bytes() for bytes.
print(f"total: {total}")
print(order)fn print[T](value T, end String = "\n") Void { ... }wrap_error Function#
Returns a copy of cause with context in front of its message, as in
error(wrap_error("read config", cause)). The kind and details are kept, and the original message is
added to details["cause"].
fn wrap_error(context String, cause Error) Error { ... }