Global functions
Rell provides a collection of built-in global functions for common tasks. For complete API documentation, see the Standard Library reference.
Overview
Global functions are available without any namespace prefix and cover:
- Math operations –
abs,min,maxforinteger,big_integer, anddecimaltypes - Collection checks –
emptyandexistsfor nullable values, lists, sets, and maps - Output –
printandlogfor debugging - Cryptographic hashes –
sha256,keccak256 - Signature verification –
verify_signature - Ethereum operations –
eth_ecrecoverfor public key recovery - Error handling –
try_callandtry_call_catchfor catching exceptions with fallback values or error inspection
Key functions
abs– Returns the absolute value of a numbermin,max– Returns the smaller/larger of two valuesempty– Checks if a value is null or an empty collectionexists– Checks if a value is non-null and non-emptyprint,log– Output to stdout or logsha256,keccak256– Cryptographic hash functionsverify_signature– Verifies a signature against a message and public keytry_call– Catches exceptions and returns a fallback valuetry_call_catch– Catchesrequireexceptions and returns a result with error info
Using empty and exists in at-expressions
Both empty() and exists() support nested at-expressions, enabling efficient SQL queries:
// Find users with no company in their city
user @* {
empty(company @* { .city == user.city })
}
// Find users with at least one company in their city
user @* {
exists(company @* { .city == user.city })
}
Use @* in nested at-expressions to execute as a single SQL query. Other cardinality operators (@+, @?, @) result
in separate queries.
Error handling with try_call and try_call_catch
The try_call function catches exceptions
and optionally provides fallback values:
val int_or_null = try_call(integer.from_hex(s, *)); // Returns null on failure
val int_or_default = try_call(integer.from_hex(s, *), -1); // Returns -1 on failure
The try_call_catch function (since
0.14.16) catches only require exceptions and returns a
try_call_result<T> that
allows inspecting the error message:
function may_fail(): integer {
require(false, "This fails");
return 0;
}
val result = try_call_catch(may_fail(*)); // Returns try_call_result with error info
Both functions restore database state on failure – any writes are rolled back. try_call catches all exceptions, while
try_call_catch only catches require exceptions and re-throws others.