Skip to main content

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 operationsabs, min, max for integer, big_integer, and decimal types
  • Collection checksempty and exists for nullable values, lists, sets, and maps
  • Outputprint and log for debugging
  • Cryptographic hashessha256, keccak256
  • Signature verificationverify_signature
  • Ethereum operationseth_ecrecover for public key recovery
  • Error handlingtry_call and try_call_catch for catching exceptions with fallback values or error inspection

Key functions

  • abs – Returns the absolute value of a number
  • min, max – Returns the smaller/larger of two values
  • empty – Checks if a value is null or an empty collection
  • exists – Checks if a value is non-null and non-empty
  • print, log – Output to stdout or log
  • sha256, keccak256 – Cryptographic hash functions
  • verify_signature – Verifies a signature against a message and public key
  • try_call – Catches exceptions and returns a fallback value
  • try_call_catch – Catches require exceptions 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
note

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.