Questions tagged [macros]

Macros are used to make a sequence of computing instructions available to the programmer as a single program statement, making the programming task less tedious and less error-prone.[

44 questions
43
votes
7 answers

Why aren't macros included in most modern programming languages?

I know that they are implemented extremely unsafely in C/C++. Can't they be implemented in a safer way? Are the disadvantages of macros really bad enough to outweigh the massive power they provide?
Casebash
  • 7,662
  • 5
  • 41
  • 62
32
votes
2 answers

What is the origin of the C Preprocessor?

The C preprocessor is attached to C, but it has a completely different syntax from the main language: syntactically significant whitespace (end of line terminates a statement, gap after the macro determines the start of the replacement…
Alex Celeste
  • 841
  • 8
  • 15
30
votes
5 answers

Are C++ templates just a kind of glorified macros?

From different comparisons among C++ templates and C#/Java generics like this one- https://stackoverflow.com/questions/31693/what-are-the-differences-between-generics-in-c-and-java-and-templates-in-c/31929#31929 I got a perception that, C++…
Gulshan
  • 9,402
  • 10
  • 58
  • 89
24
votes
6 answers

How useful are Lisp macros?

Common Lisp allows you to write macros that do whatever source transformation you want. Scheme gives you a hygienic pattern-matching system that lets you perform transformations as well. How useful are macros in practice? Paul Graham said in…
compman
  • 1,387
  • 13
  • 21
22
votes
4 answers

What about LISP, if anything, makes it easier to implement macro systems?

I'm learning Scheme from the SICP and I'm getting the impression that a big part of what makes Scheme and, even more so, LISP special is the macro system. But, since macros are expanded at compile-time, why don't people make equivalent macro systems…
20
votes
4 answers

Python decorators and Lisp macros

When looking Python decorators someone made the statement, that they are as powerful as Lisp macros (particularly Clojure). Looking at the examples given in PEP 318 it looks to me as if they are just a fancy way of using plain old higher-order…
Profpatsch
  • 969
  • 8
  • 13
20
votes
19 answers

Programming languages with a Lisp-like syntax extension mechanism

I have only a limited knowledge of Lisp (trying to learn a bit in my free time) but as far as I understand Lisp macros allow to introduce new language constructs and syntax by describing them in Lisp itself. This means that a new construct can be…
Giorgio
  • 19,486
  • 16
  • 84
  • 135
20
votes
1 answer

Why does the C library use macros and functions with same name?

I am reading 'The Standard C Library' by PJ Plauger which is really interesting. The book explains not only how to USE the library but also how it is implemented. I have finished reading the ctype.h section and in the header the functions are…
user619818
  • 1,757
  • 2
  • 14
  • 23
20
votes
2 answers

What are the typical applications of Lisp macros?

I am trying to learn some LISP and I have read a lot about the importance of LISP macros so I would like to get some working experience with them. Can you suggest a practical application area that would allow me to use macros to solve a real-world…
Giorgio
  • 19,486
  • 16
  • 84
  • 135
13
votes
3 answers

Is using C/C++ macros as a shortcut for conditional compilation a good practice?

Let's say I want to have several types of output messages in my code. One of them is DEBUG, which is printed only, when the code is compiled in Debug mode. Usually I'd have to write something like #ifdef DEBUG std::cout << "Debug message" <<…
Eenoku
  • 327
  • 1
  • 9
12
votes
1 answer

What practical problem results from lack of hygienic macros in Clojure?

I've heard that Clojure macros are easier to write but not as reliable as Racket's hygienic macros. My question has 2 parts: How does gensym differ from hygienic macros? What do Racket macros provide that Clojure's don't? (be it safety,…
Alex
  • 284
  • 1
  • 4
11
votes
7 answers

How can a compiler be written for a language that allows rewriting code at runtime (such as Lisp macros)?

There are some programming languages, like the many dialects of Lisp, that allow for macro-metaprogramming: rewriting and altering sections of code before the code is run. It is relatively trivial to write a simple interpreter for Lisp (mostly…
Qqwy
  • 4,709
  • 4
  • 31
  • 45
11
votes
3 answers

Byte code weaving vs Lisp macros

I have been reading about the libraries people have written for languages like Java and C# that make use of byte code weaving to do things like intercept function calls, insert logging code, etc. I have also been reading up on Lisp/Clojure macros in…
mortalapeman
  • 1,613
  • 9
  • 20
10
votes
3 answers

Is macros support in a programming language considered harmful?

The first abuse that comes to my mind in C is: #define if while But at the same time it is extremely handy and powerful when used correctly. Something similar happens with Common Lisp macros. Why doesn't all the programming languages support…
OscarRyz
  • 1,675
  • 3
  • 15
  • 26
8
votes
3 answers

Idiomaticy of macros in C++

Macros are considered a good thing by one and evil by another. Is there a rule of thumb when to and when not to use macros in C++? When are macros idiomatic and when should they be avoided?
Mast
  • 201
  • 2
  • 12
1
2 3