Skip to main content

Enum

Enumeration types, or enums, creates maintainable sets of constants in Rell, enhancing code readability and preventing errors.

enum account_type {
single_key_account,
multi_sig_account
}

entity account {
key id: byte_array;
mutable account_type;
mutable args: byte_array;
}

Enum declaration​

Declaring an enum involves defining a named collection of related constants. In Rell, each constant is associated with a unique integer value based on its position within the enum. Here's an example of an enum declaration for different currency types:

enum currency {
USD,
EUR,
GBP
}

print(currency.values()); // [USD, EUR, GBP]

In this case, currency is an enum containing the constants USD, EUR, and GBP.

Enum usage​

Once an enum is declared, its constants can be used to represent distinct values within your program. For instance:

enum currency {
USD,
EUR,
GBP
}

var c: currency;
c = currency.USD;
print(c); // USD

This snippet demonstrates assigning the currency.USD constant to a variable c.

enum value properties:

  • .name: text - the name of the enum value.
  • .value: integer - the numeric value (index) associated with the enum value.

Functions available for all enum types:

FunctionDescription
T.values(): list<T>Returns all values of the enum in the order of declaration.
T.value(text): TFinds a value by name, gives an exception if not found.
T.value(integer): TFinds a value by index, gives an exception if not found.
Example​
enum currency {
USD,
EUR,
GBP
}

val cs: list<currency> = currency.values(); // Returns all values (in the order in which they are declared)
print(cs); // [USD, EUR, GBP]

val eur = currency.value("EUR"); // Finds enum value by name
val gbp = currency.value(2); // Finds enum value by index
print(eur, gbp); // EUR GBP

val usd_str: text = currency.USD.name; // Returns 'USD'
val usd_value: integer = currency.USD.value; // Returns 0
print(usd_str, usd_value); // USD 0
  • currency.values(): Retrieves all enum values as a list, maintaining their order of declaration.
  • currency.value('EUR'): Finds the enum value corresponding to the provided name (e.g., 'EUR').
  • currency.value(2): Retrieves the enum value based on its index (e.g., the third value, GBP).
  • currency.USD.name: Returns the name of the enum constant (e.g., 'USD').
  • currency.USD.value: Returns the integer value associated with the enum constant (e.g., 0 for USD).