Enumerations
Enumerations in Adama are simply ways of associating integers to names.
enum Suit { Hearts:1, Spades:2, Clubs:3, Diamonds:4 }
We can refer to a single value via :: by
Suit x = Suit::Hearts;
Enumeration collections
We can build an array of all the values within an enumeration using the * symbol. For example, we can build an array of all the suit types via:
Suit[] all = Suit::*;
which is a handy. We can also build an array of all enumeration values which share a prefix. For instance, given the enum
enum Role { CoreLeft, CoreRight, Normal };
then we can refer to the collection of all values that start with Core via
Role[] core = Role::Core*;
This is a handy way to build a strict taxonomy within an enumeration.
Dispatch
Dispatching via enums is a way to split complex functions based on enumerations with an implicit switch statement.
Suppose we have an enumeration with three values:
enum X { Open, Hay, Stubble }
record Item {
X resource;
int count;
}
We can build a dispatcher function called tick which accepts an item.
dispatch X::Open tick(Item item) {
// do a thing for the Open resource
}
dispatch X::Hay tick(Item item) {
// do a thing for the Open resource
}
dispatch X::Stubble tick(Item item) {
// do a thing for the Open resource
}
The tick function then becomes a property of the X enumeration such that
Item item;
#sm {
item.resource.tick(item);
}
This creates a simple way of doing dispatch for rules per enumeration value. This statically creates an error when an enum value is created when the associated dispatcher function hasn't been written. For example, if we add the value What to the enum, then the following error is
Enum 'X' has a dispatcher 'tick' which is incomplete and lacks: What.