Exploring macro hooks
Macro hooks are special macro definitions that are invoked by the Crystal compiler in some situations at compile time. These include the following:
inherited
is invoked when a subclass is defined, where@type
is the inheriting type.included
is invoked when a module is included, where@type
is the including type.extended
is invoked when a module is extended, where@type
is the extending type.method_missing
is invoked when a method is not found and is passed a singleCall
argument.method_added
is invoked when a new method is defined in the current scope and is passed a singleDef
argument.finished
is invoked after the semantic analysis phase, so all the types and their methods are known.
The first three and finished
definitions are the most common/useful ones, so we are going to focus on those here. The first three hooks all work essentially the same – they just execute in different contexts. For example, the following...