The Artima Developer Community
Sponsored Link

Heron-Centric: Ruminations of a Language Designer
Macros and Type-Systems
by Christopher Diggins
October 2, 2005
Macros are a frequently overlooked yet tremendously useful language feature. What if we give them a more modern twist?


Any selection of imperative language constructs a language designer comes up with is completely arbitrary, and is going to be limiting in one way or another. I think the best way to deal with this problem is to provide macros. When I talk about macros I am not talking about the nasty C pre-processor, but something more closely resembling macros in MacroML or Lisp.

I envision macros being well-integrated into the language and part of the type system. For instance it makes sense to me to be able to pass macros as template parameters. It also makes sense for macro parameters to be typed.

Consider the following code:

int x;
repeat (42) { ++x; }
This is not valid Heron code, but it makes sense. Instead of adding a new language construct my plan is to allow the programmer to define it as a macro as follows:
macro repeat('int A, 'code B)
  _cond(A <= 0, {}, true, ($B, repeat(A - 1, B)));
So as you may realize the macro is defined using a functional meta-language. _cond is the same as the Lisp function COND. The 'int represents a meta-int value, which is equivalent to a constant literal integer value. The 'code represents a statement or code block. The above example clearly won't work in the following scenario:
int x;
repeat (read_int()) { ++x; }
However, a new overloaded repeat macro can be defined as follows:
macro repeat(int A, 'code B)
    int _i = A;
    while (_i > 0) {
Any thoughts or suggestions? Think it is time for better macros and code-rewriting tools in modern languages?

Talk Back!

Have an opinion? Readers have already posted 35 comments about this weblog entry. Why not add yours?

RSS Feed

If you'd like to be notified whenever Christopher Diggins adds a new entry to his weblog, subscribe to his RSS feed.

About the Blogger

Christopher Diggins is a software developer and freelance writer. Christopher loves programming, but is eternally frustrated by the shortcomings of modern programming languages. As would any reasonable person in his shoes, he decided to quit his day job to write his own ( ). Christopher is the co-author of the C++ Cookbook from O'Reilly. Christopher can be reached through his home page at

This weblog entry is Copyright © 2005 Christopher Diggins. All rights reserved.

Sponsored Links


Copyright © 1996-2018 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use