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
}
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:
var c: currency;
c = currency.USD;
This snippet demonstrates assigning the currency.USD
constant to a variable c
.
enum
value properties:
.name: text
- the name of theenum
value..value: integer
- the numeric value (index) associated with theenum
value.
Functions available for all enum
types:
Function | Description |
---|---|
T.values(): list<T> | Returns all values of the enum in the order of declaration. |
T.value(text): T | Finds a value by name, gives an exception if not found. |
T.value(integer): T | Finds a value by index, gives an exception if not found. |
Example
val cs: list<currency> = currency.values() // Returns all values (in the order in which they are declared)
val eur = currency.value('EUR') // Finds enum value by name
val gbp = currency.value(2) // Finds enum value by index
val usd_str: text = currency.USD.name // Returns 'USD'
val usd_value: integer = currency.USD.value // Returns 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
forUSD
).