The component system is one of two options for composition over inheritance, the other being traits (#12). The component system is based on the delegation model and shared many similarities, but the primary one is that it allows for the composition of any given class instance to be changed at runtime.
Basics
In order for a class to have components, the class must first specify that it can have components and what kinds of components there are. To do this, a special component variable is defined in the class along with the kind annotation; functions are then annotated with this annotation to specify that function calls will automatically be delegated to the respective component.
Overview
The component system is one of two options for composition over inheritance, the other being traits (#12). The component system is based on the delegation model and shared many similarities, but the primary one is that it allows for the composition of any given class instance to be changed at runtime.
Basics
In order for a class to have components, the class must first specify that it can have components and what kinds of components there are. To do this, a special
component
variable is defined in the class along with the kind annotation; functions are then annotated with this annotation to specify that function calls will automatically be delegated to the respective component.We then specify the components using the same annotations and then override the respective functions: