Dodo doc > Macros and Templates > Macro Function


Macro Function

Syntax

A macro function also uses "wrap". The function name as selector, the arguments and the continuations are captured using pattern matching. By default the argument list is empty which matches a constant or a function without arguments. By default there are two continuations, "return" and "throw".

Another captured item is the scope of the function declaration which is "instance" by default but can also be "type".

The macro block contains a substitution for the body of the function. The selector can be used to call the matched function with "apply" and a list of arguments. Note that a macro function without a specific list of argument types is only called if the function name and the argument list do match an existing declaration so that "apply" works.

Examples:

wrap (selector: $f, arguments: $args):
   ++.link!counter
   return f.apply(args)
.

wrap (selector: delete, arguments: [?/House])
{
   # delete(House): don't do anything
}

If the selector starts with a capital letter it matches a method or constructor. In that case the macro does not return a value but it can modify "self" which is returned to the caller. Arguments passed by reference can be modified in the argument list. The selector can be used directly to call the matched method or constructor. The list of arguments should be passed by reference.

Rationale: it is common to use macros that behave like functions and this syntax allows it. The selector can match an existing function to replace it or add logic ("wrap") around it.
Since there can be a different list of arguments depending on the function they are captured as a list. This dictates a different way to call a wrapped function with arguments.
The compiler needs to know the type of the arguments so either the macro must list the types or it must be an existing function with known argument types.

Declaration and use

A macro function declaration follows the same rules as a function declaration. This includes polymorphism: a macro function can be overridden in a subtype. However unlike a function, the selector is not prefixed with "^" to indicate overriding.

A macro function is used in the same way as a function. See chapter on functions.

Rationale: to work better as a wrapper for functions, macro functions must support overriding. But the overriding mark "^" would become more complex if macro functions are involved. It is a design goal to make macro function calls same as function calls.

^ 5.1. Simple Macro

v 5.3. Advanced Macro