Monolithic Entities: The Basics
The simplest form of an entity in Flint is a monolithic entity, where data and functionality are defined together in one place.
Example:
entity Counter:
data:
int value;
func:
def increment(int amount):
value += amount;
def get_value() -> int:
return value;
Counter(value); // Constructor to initialize the entity
Key Points:
- Structure: Entities have two main sections:
- Data: Variables that store the state of the entity.
- Func: Functions that operate on the data.
- Constructor: The constructor initializes the entity. Its parameters must match the declared fields in the
data
section. - Use Case: Monolithic entities are straightforward and perfect for small projects or tightly coupled logic.
- Immutability: The data saved inside an entity cannot be accessed outside of func modules. So, when Entity
E
uses a func module which acts on dataD
the data ofD
cannot be accessed via an instance ofE
. This means thate.value
is impossible. For all operations on data, getters and setters must be provided inside thefunc
module.
Usage Example:
def main():
Counter counter = Counter(0);
counter.increment(5);
print($"Counter value: {counter.value}");
counter.reset();
print($"Counter value after reset: {counter.value}");
Monolithic entities are Flint’s simplest abstraction, but their true power emerges as we explore func modules.