Skip to content

Builtin Functions ​

These are the functions that are defined in the global namespace, they have no module prefix and are the core functions of the language, its flow, and error handling.

import ​

import(path: str) -> any|err

Loads a solus file and then executes it. If the file is not found at first and the .solu file extension is not provided, it will attempt to find path + ".solu", and path + ".solus".

Arguments ​

ParameterTypeDescription
pathstrFile path to import

Example ​

solus
var module = import('module').unwrap();
io.println(module.type() == 'err' and module or module.version);

require ​

require(path: str) -> any|panic

Loads a solus file and then executes it, behaving identically to import besides the fact that it panics instead of returning an error.

Arguments ​

ParameterTypeDescription
pathstrFile path to import

Example ​

solus
var module = require('notfound'); # This will stop execution!
var mod2 = catch([]() require('notfound')); # Same as import('notfound')

include ​

include(path: str) -> any

Technically not a function but rather a keyword, this includes a file in the compilation path of the file it is called in, therefore resulting in a single chunk of bytecode.

Arguments ​

ParameterTypeDescription
pathstrFile path to include

Example ​

solus
var module = include('notfound'); # This will fail at compile time

eval ​

eval(src: str) -> any|err

Compiles source code from a string into a fun, note that this is NOT sandboxed and is NOT safe to accept use input with. Treat eval very carefully.

Arguments ​

ParameterTypeDescription
srcstrsolus source code

Example ​

solus
var cat = "{ meows = 2 paws = 4 }";
var instance = eval(cat);
io.println(instance.meows); # 2

panic ​

panic(err: str) -> panic

Creates a runtime panic, solus’ equivalent to an exception pretty much. This will exit the program if it is not DIRECTLY caught.

Arguments ​

ParameterTypeDescription
errstrThe reason for failure

Example ​

solus
if fatal_err_condition
    panic("ohhhh shit!");

catch ​

catch(try: fun) -> any|err

Catches any panics thrown within the provided try block’s scope of execution. Think of this as a try/catch that returns a result instead of passing it.

Arguments ​

ParameterTypeDescription
tryfunA function to attempt

Example ​

solus
var mod = catch([]() require('notfound')); # Same as import("notfound")

attempt ​

attempt(try: fun, handler: fun) -> any

Similar to catch except it uses a proper handler fun, mimicking try/catch behavior in other languages. If try returns err or panics, handler will be passed the err.

Arguments ​

ParameterTypeDescription
tryfunA function to attempt first
handlerfunA function to call on failure

Example ​

solus
var mod = attempt(
    []() import("doesnt-exist.solu"),
    [](err) { io.println(err); {} }
);

unwrap ​

unwrap(val: any) -> any|panic

Unwraps a variable that is ambiguously value or error, returning either the value or panicking on err

Arguments ​

ParameterTypeDescription
valany errEither an error or a value

Example ​

solus
var mod = import('').unwrap(); # Panics on failure like require("")

or_else ​

or_else(valid: any, invalid: any) -> any

Returns valid if it is a valid value, or returns invalid if valid is err

Arguments ​

ParameterTypeDescription
validany errEither an error or a value
invalidanyThe default or fallback value

Example ​

solus
var cfg = io.fread('test.cfg').or_else(default);

then ​

then(self: any, handler: fun) -> any

Perform an operation on a value using a handler function. Useful for function chaining

Example ​

solus
var cfg = io.fread('test.cfg').or_else(default).then([](cfg) {
    io.println($'Version: {cfg.version}');
    io.println($'Color: {cfg.color}');
});

assert ​

assert(con: bool) -> nil|panic

Throws a panic if the provided condition is false

Arguments ​

ParameterTypeDescription
conboolA boolean condition to assert

Example ​

solus
assert(1 == 1);
assert(1 == 2); # Panic

type ​

type(val: any) -> str

Returns the type of a value represented as a string

Arguments ​

ParameterTypeDescription
valanyAny value possible in solus

Example ​

solus
var module = import("module").unwrap();
if module.type() == 'err'
    io.println("Failed to load module.solu");
else
    io.println(module.version);

str ​

str(val: any)

Converts a value of any type into a string representation. Note that this function does not stringify objects, but rather gives a string representation of the pointer.

Arguments ​

ParameterTypeDescription
valanyAny value possible in solus

Example ​

solus
var x = 4; # i64
var y = {}; # obj
io.println("x: " + str(x) + ", y: " + y.str(); # "x: 4, y: 0x71d00c0d8"

err ​

err(str: str) -> err

Constructs an err type from a str. You can return this to represent failure conditions.

Arguments ​

ParameterTypeDescription
strstrThe reason for failure

Example ​

solus
var fun = [](bool) {
    bool and "success"
    or err("failure!")
};

i64 ​

i64(conv: f64|str)

Converts an f64 value into the i64 equivalent value. This will result in loss of floating point (decimal) precision.

Arguments ​

ParameterTypeDescription
convf64 strThe value to convert to i64

f64 ​

f64(conv: i64|str) -> f64

Converts an i64 value into the f64 equivalent value. Not all values i64 represents can be represented by f64 accurately.

Arguments ​

ParameterTypeDescription
convi64 strThe value to convert to f64