Sponsored Link •
|
Summary
Macros are a frequently overlooked yet tremendously useful language feature. What if we give them a more modern twist?
Advertisement
|
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) { $B --_i; } }Any thoughts or suggestions? Think it is time for better macros and code-rewriting tools in modern languages?
Have an opinion? Readers have already posted 35 comments about this weblog entry. Why not add yours?
If you'd like to be notified whenever Christopher Diggins adds a new entry to his weblog, subscribe to his RSS feed.
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 ( www.heron-language.com ). Christopher is the co-author of the C++ Cookbook from O'Reilly. Christopher can be reached through his home page at www.cdiggins.com. |
Sponsored Links
|