Macros - vilinski/nemerle GitHub Wiki

Table of Contents

Intro

You can think about macros as of a system of compile-time transformations and automatic generation of code with regard to some rules. It can be used either to automate manipulations performed on similar data-types and fragments of code or to add syntax shortcuts to the language, optimize and make some computations safer by moving them from runtime to compile-time.

The idea of making simple inline operations on the code comes from preprocessor macros, which many languages (especially C, C++) have contained since early times of compiler design. We are following them in the direction of much more powerful, and at the same time more secure (type-safe) solutions like Haskell Template Meta-programming.

Key features

  • built-in ability of extending language syntax
  • algorithmic generation of code, which may be dependent on external factors, like XML files, script programs, databases or even remote web sites
  • completing datatypes with automatically created methods for performing arbitrary operations, e.g. reading datasets from a file, sending them through the network or storing in an SQL database
  • using the context of compilation provided by compiler's internal structures, like line numbers (for error reporting), names of processed type, function, class or namespace and accessing datatype definitions by name
  • creating functions, whose amount and type of arguments depend on each other (like C printf) with full type-checking during compile-time

Using macros

You can take a look at the most appealing examples of macro use. It contains a description of how we incorporate methodologies as Design By Contract, compile-time verification of SQL statements and Aspects-Oriented Programming into Nemerle.

Most useful macros usually gets into the standard tool library of application. Nemerle compiler also have such macros, and they are by default available together with the distribution. Their list is kept on class library page.

Writing macros

Articles

⚠️ **GitHub.com Fallback** ⚠️