Func Modules: Modularizing Behavior
Func modules separate behavior from entities, making Flint entities highly modular. They act upon data and can be reused across multiple entities. A func module defines functionality independently of a specific entity.
Syntax Recap:
data CounterData:
int value = 0;
CounterData(value);
func CounterActions requires(CounterData c);
def increment(int amount):
c.value += amount;
def get_value() -> int:
return c.value;
Key Points:
- Requires Keyword: The
requires
keyword specifies the data module(s) this func module operates on. In this case, it requires a data module of typeCounter
. This data module has to be present on every entity that uses theCounterActions
func module. - Reusability: A single func module can be shared across multiple entities that use compatible data structures.
- Immutability: Just like with monolithic entities, 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.
By separating behavior from data, Flint encourages reuse and cleaner code. But how do these fit into entities?