3 ms·
Macros are still mindblowing, but much less so if you've used Ruby or Python or Perl than if you're a pure C programmer. I disagree with this statement. Python
by BrandonM 14y ago
Macros are still mindblowing, but much less so if you've used Ruby or Python or Perl than if you're a pure C programmer.
I disagree with this statement. Python's introspection, first-class functions, magic methods, and duck-typing add a lot of the dynamism of Lisp, but every time you run up against something that needs to be a macro, it's a dead end. C has a rudimentary macro system that can get you a little past that point; for example, it's not too difficult to add a foreach construct to C that looks like this:
queue *q = queue_new();
queue_add(q, 1);
/* ... */
foreach (int i, q) {
printf("%z\n", i);
}
On a sidenote, it's kind of sad that a programming layer just above assembly language created 4 decades ago has better macro support than some of the most popular modern languages.
- dkersten 14y agoOn a sidenote, it's kind of sad that a programming layer just above assembly language created 4 decades ago has better macro support than some of the most popular modern languages. I disagree. In Python, you have much more advanced metaprogramming facilities, depending on what kind of effort you wish to go to. You have the normal introspection, magic methods and metaclasses as you mentioned (which, IMHO, are in themselves already much more powerful than what you can do with the C preprocessor). But you also have access to exec and eval which lets you do all kinds of crazy stuff that isn't possible with C macros. Using operator overloading and classes, you can even create a lot of new syntax which isn't possible in C[1]. Finally, if you are really determined, you can even hack semantics of existing Python constructs by instrumenting the bytecode. For example, I saw a hack which adds tail-call optimization to Python functions this way (iirc as a decorator). [1] One example is python-pipeline: http://code.google.com/p/python-pipeline/ http://code.google.com/p/python-pipeline/