Skip to content

Globals Reference

Creates an array of the provided length and initializing each element to its index.

var arr1 = array_of(3); // [0, 1, 2]
var arr2 = array_of(3, (i) -> i + 1); // [1, 2, 3]

length: Number - The length of the array.

initializer: (Number) -> dyn! optional - An optional function that takes the index as a parameter and sets the element in the array to its return.

[dyn!] - An array of the specified length where each element is either its index or the result of the initializer function.

Creates an array of the provided length.

var arr = array_of_length(3); // [nil, nil, nil]

length: Number - The length of the array.

[dyn?] - An array of the specified length filled with nil values.

Asserts the value is truthy. Ends the program if not.

assert(true); // Assertion is true, so nothing happens.
assert(false, "This is going to error."); // Assertion is false, the program exits, giving an error using the message if provided.

value: dyn! - Any value. Falsy values (0 and nil) cause the program to exit.

message: String? optional - An optional message if the assertion fails.

nil

Throws an error if the value is false or nil.

Asserts two values are equal. Ends the program if not.

assert_eq(1, 1); // Assertion is true, so nothing happens.
assert_eq(1, 0, "This is going to error."); // These values are not equal, so the assertion fails and the program exits.
#### Parameters
value1: `dyn!` - The first value to check for equality.
value2: `dyn!` - The second value to check for equality.
message: `String?` <Badge text="optional" /> - An optional message if the assertion fails.
#### Returns
`nil`
#### Throws
Throws an error if the values are not equal.
### `assert_not_nil`
Asserts if that a value is not `nil`. Useful as a typeguard.
```ql
var value1 = true;
assert_not_nil(value1); // Nothing happens because value1 is not nil
var value2 = nil;
assert_not_nil(value2); // Assertion is false so this throws, exiting the program.

function: dyn! - The value to check for nil.

message: String? optional - An optional message if the assertion fails.

nil

Throws an error if the provided function does not throw an error.

Asserts a function will throw an error. If no error is thrown, the program exits with an error.

assert_throws(() -> 1 / 0); // Nothing happens as 1 / 0 throws an error.
assert_throws(() -> println("Hello World", "This is going to error.")); // This callback will not throw, so the program will exit.

function: () -> dyn! - The function to call to check for an error.

message: String? optional - An optional message if the assertion fails.

nil

Throws an error if the provided function does not throw an error.

An identity function.

var value = identity(1); // 1

value: dyn! - Any value.

dyn! - The value provided in the argument.

Prints a value to the console. All values are implicitly converted to strings.

print("Hello World!"); // prints Hello World!

value: dyn! - Any value.

nil

Prints a value to the console followed by a newline. All values are implicitly converted to strings.

println("Hello World!"); // prints Hello World!\n

value: dyn! - Any value.

nil

Returns the value as a string.

var one_string = to_string(1); // "1"

value: dyn! - Any value.

String - The value as a string.

Returns the type as a string.

println(typeof("Hello World!")); // string

value: dyn! - Any value.

String - a value of "nil", "boolean", "number", "string", "function", "class", "object", "array" or "module" depending on the type of the value.

Returns the time in milliseconds since the start of the Unix epoch.

var start = system_time();
complex_operation();
var end = system_time();
var duration = start - end; // The time in milliseconds between start and end.

None

Number - The time in milliseconds since the start of the Unix epoch.

a constant containing the string "array", used with the typeof function can return the runtime type.

assert([] is ARRAY);
assert_eq(typeof([]), ARRAY);

a constant containing the string "boolean", used with the typeof function can return the runtime type.

assert(true is BOOLEAN);
assert_eq(typeof(false), BOOLEAN);

a constant containing the string "class", used with the is operator or typeof function can return the runtime type.

class MyClass { }
assert(MyClass is CLASS);
assert_eq(typeof(MyClass), CLASS);

a constant containing the string "function", used with the is operator or used with the typeof function can return the runtime type.

assert(() -> nil is FUNCTION);
assert_eq(typeof(() -> nil), FUNCTION);

a constant containing the string "module", used with the is operator or used with the typeof function can return the runtime type.

mod utils = import("./utils.ql");
assert(utils is MODULE);
assert_eq(typeof(utils), MODULE);

a constant containing the string "nil", used with the is operator or used with the typeof function can return the runtime type.

assert(nil is NIL);
assert_eq(typeof(nil), NIL);

a constant containing the string "number", used with the is operator or used with the typeof function can return the runtime type.

assert(0 is NUMBER);
assert_eq(typeof(1), NUMBER);

a constant containing the string "object", used with the is operator or used with the typeof function can return the runtime type.

assert({{ }} is OBJECT);
class MyClass { }
assert(MyClass() is OBJECT);
assert_eq(typeof(MyClass()), OBJECT);

a constant containing the string "string", used with the is operator or used with the typeof function can return the runtime type.

assert("" is STRING);
assert_eq(typeof(""), STRING);