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".

Parameters

ParameterTypeDescription
pathstrFile path to import

Example

solus
var module = unwrap(import("module"));
if type(module) == "err"
    io.println("Failed to load module.solu");
else
    io.println(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.

Parameters

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.

Parameters

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.

Parameters

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)

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

Parameters

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.

Parameters

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 fails or panics, handler will be passed the err.

Parameters

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

Example

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

unwrap

unwrap(val: any) -> any|panic

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

Parameters

ParameterTypeDescription
valany errEither an error or a value

Example

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

unwrap_or

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

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

Parameters

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

Example

solus
var cfg = unwrap_or(do {
    var f = io.fread("test.cfg");
    type(f) == "err" and f or eval(f)
}, do {
    unwrap(io.fwrite("test.cfg", obj.stringify(default, false)));
    default
});

assert

assert(con: bool) -> nil|panic

Throws a panic if the provided condition is false

Parameters

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

Parameters

ParameterTypeDescription
valanyAny value possible in solus

Example

solus
var module = unwrap(import("module"));
if type(module) == "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.

Parameters

ParameterTypeDescription
valanyAny value possible in solus

Example

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

err

err(str: str) -> err

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

Parameters

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.

Parameters

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.

Parameters

ParameterTypeDescription
convi64 strThe value to convert to f64