Getting Started with EK9 · Appendix B
Constructs at a Glance
EK9 has a small family of building blocks. You have met all of them in this book. This appendix puts their capabilities side by side, so you can look up "can a record have methods?" or "does a trait hold state?" in one place.
In the table, $\checkmark$ means "yes, has this"; -- means "no". Read it together with the
chapter each construct is taught in, which gives the exact rules.
| Construct | Fields | Methods | Operators | Constructors | Can be abstract | Can be extended |
|---|---|---|---|---|---|---|
| Function | -- † |
-- |
-- |
-- |
$\checkmark$ | ‡ |
| Program | -- |
-- |
-- |
-- |
-- |
-- |
| Record | $\checkmark$ (public) | -- |
$\checkmark$ | $\checkmark$ | $\checkmark$ | $\checkmark$ |
| Class | $\checkmark$ (private) | $\checkmark$ | $\checkmark$ | $\checkmark$ | $\checkmark$ | $\checkmark$ |
| Trait | -- |
$\checkmark$ | $\checkmark$ | -- |
always | $\checkmark$ |
| Component | $\checkmark$ (private) | $\checkmark$ | $\checkmark$ | $\checkmark$ | $\checkmark$ | $\checkmark$ |
| Constrained type | (its base) | (its base) | (its base) | $\checkmark$ | -- |
-- § |
| Enumeration | fixed members | -- |
compare, text, first/last | from text | -- |
-- |
† A plain function holds no state. The one exception is a dynamic function with a capture list: the captured values become its fields (the second volume).
‡ A function is given an implicit super-type by the compiler so it can be passed around and sorted (the second volume). You reuse behaviour by passing functions, not by subclassing them.
§ A constrained type constrains a base type (Constrained Types). It is not a subtype of its base and cannot be constrained further.
A few things the table cannot show:
- Records hold data; classes hide it. A record's fields are public — it is a transparent carrier of values. A class's fields are private — it hides how it stores things behind its methods. That is the difference in one line.
- A component hides a type. A class hides values; a component hides a whole implementation behind an interface (Components), and an application wires the real one in (the second volume). Its behaviour is reached through its methods.
- A trait carries behaviour, not state. It has no fields of its own; its methods and any operators run on the classes that adopt it (Traits and Composition).
- Every construct with fields needs
?. Any aggregate that holds data must be able to say whether it is set, so it needs the?operator — write your own, or ask fordefault operator ?.
In the EK9 reference: Design note: what EK9 leaves out