package core
Pair :: struct (First_Type: type_expr, Second_Type: type_expr) {
first: First_Type
second: Second_Type
}
A Pair
represents a pair of values of heterogenous types. This structure does not do much on its own; however, it is useful because provides overloads for formatting, hashing and equality. This means you can use a Pair(T, R)
as a key for a Map or Set out of the box, provided T and R are hashable and equatable.
cptr :: struct (T: type_expr) {
data: u64
}
cptr.read :: (this: cptr(void)) -> void
cptr.read :: (this: cptr($T)) -> T
cptr.read :: (this: cptr($T), buffer: [] u8) -> void
Result :: union (Ok_Type: type_expr, Err_Type: type_expr) {
Err: Err_Type
Ok: Ok_Type
}
Result(T, E) is a structure that represents either an Ok value of type T, or an Err value of type E. status
contains either .Ok, or .Err depending on which is currently held.
Result.and_then :: (r: Result, f: (r.Ok_Type) -> Result($R, r.Err_Type)) -> Result(R, r.Err_Type)
Monadic chaining operation.
Result.catch :: macro (r: Result($T, $E), on_err: Code) -> T
If result contains Err, the given code is run. This code is expected to either:
- Return a good value with
return
- Return an error value with
return return
This procedure is subject to change.
Result.expect :: (r: Result, msg: [] u8) -> r.Ok_Type
Tries to extract the Ok value out of the Result. If the result contains an Err, a custom assertion message is thrown.
Result.forward_err :: macro (r: Result($T, $E)) -> T
If result contains Err, the error is returned from the enclosing procedure. Otherwise, the Ok value is returned.
f :: () -> Result(i32, str) {
return .{ Err = "Oh no..." };
}
g :: () -> Result(str, str) {
// This returns from g with the error returned from f.
v := f()->forward_err();
println(v);
return .{ Ok = "Success!" };
}
Result.or_else :: (r: Result, generate: () -> unknown) -> #auto
If the Result contains Err, generate is called to make a value
Result.or_return :: macro (r: Result($T, $E), v: $V) -> T
If result contains Err, the given value is returned from the enclosing procedure. Otherwise, the Ok value is returned.
Result.transform :: (r: Result($T, $E), f: (T) -> $R) -> Result(R, E)
Returns a new result defined by:
Ok(n) => Ok(f(n))
Err(e) => Err(e)
Result.transform_err :: macro (r: Result($T, $E), f: (E) -> $N) -> Result(T, N)
If result contains Err, the error is mapped to a new error type.
Result.unwrap :: (r: Result) -> r.Ok_Type
Forcefully extracts the Ok value out of the Result. If the result contains an Err, an assertion is thrown.
print :: (x: [] u8) -> void
print :: (x: $__type_x) -> #auto
print :: (x: $__type_x, y: $__type_y) -> #auto