Skip to main content

Require function

The require function allows you to verify Boolean conditions, check for null values, or ensure non-empty collections.

Variants

Checking Boolean conditions

  • require(boolean, text): Throws an exception if the boolean argument is false, using the provided text as the error message.

Checking for null values

  • require(T?, text): T - gives an exception if the argument is null; otherwise returns the argument.
  • require_not_empty(T?, text): T - same as the previous one.

Checking for empty collections

  • require_not_empty(list<T>, text): list<T> - gives an exception if the argument is an empty list; otherwise it returns the list.
  • require_not_empty(set<T>, text): set<T> - gives an exception if the argument is an empty set; otherwise it returns the set.
  • require_not_empty(map<K,V>, text): map<K,V> - gives an exception if the argument is an empty map; otherwise, it returns the map.

Nullable collections

  • When passing a nullable collection to require_not_empty, it throws an exception if it is either null or empty.

Example

val x: integer? = calculate();
val y = require(x, "x is null"); // type of "y" is "integer", not "integer?"

val p: list<integer> = get_list();
require_not_empty(p, "List is empty");

val q: list<integer>? = try_to_get_list();
require(q); // fails if q is null
require_not_empty(q); // fails if q is null or an empty list