Are preprocessors obsolete in modern languages?

I make a simple compiler for a simple animal language, which I create based on the C-background (although I write it in Ruby). I wondered if a preprocessor was needed.

What do you think? Is a "dumb" preprocessor still necessary in modern languages? Were conditional C # compilation capabilities considered a β€œpreprocessor”? Does every modern language that does not include a preprocessor have utilities necessary for its proper replacement? (for example, the C ++ preprocessor is now mostly deprecated (although still dependent on) due to patterns.)

+6
language-agnostic preprocessor language-design
source share
8 answers

C preprocessing can do really neat things, but if you look at what it used for you, you often understand this by simply adding another layer of abstraction.

  • Preprocessing for different operations on different platforms? This is basically an abstraction layer for platform independence.
  • Preprocessing to easily add complex code? Abstraction because language is not general enough.
  • Preprocessing to add extensions to your code? Abstraction because your code / your language is not flexible enough.

So my answer is: you do not need a preprocessor if your language is high enough *. I would not call preprocessing evil or useless, I just say that the more abstract a language becomes, the less reason I can think that it needs preprocessing.

* What high level is enough? This, of course, is completely subjective.

EDIT: Of course, I really mean macros . Using preprocessors to interact with other code files or to determine constants is evil.

+8
source share

A preprocessor is a cheap method that allows you to seamlessly provide incomplete metaprogramming tools to a language.

Prefer metaprogramming or the Lisp macro instead.

+7
source share

A preprocessor is not needed . For real metaprogramming, you should have something like MetaML or Template Haskell or hygienic macros & agrave; la Scheme. For quick and dirty stuff, if your users must have it, always m4 .

However, a modern language must support the equivalent of C #line directives. . Such directives allow the compiler to find errors in the original source, even if this source is built into the parser generator or the lexer generator or a competent program. In other words,

  • Build your own language so you don't need a preprocessor.
  • Do not associate your language with a blissful preprocessor.
  • But if others have their own reasons for using the preprocessor (parser generation is popular), provide support for accurate error messages.
+5
source share

I think preprocessors are a crutch to support a language with poor expressive power.

I have seen so many abuses of preprocessors that I hate them with passion.

+2
source share

A preprocessor is a split compilation phase. Although preprocessing can be useful in some cases, the headaches and errors that it can cause create a problem.

In C, the preprocessor is mainly used for:

  • Data inclusion. While the powerful, most common precedents do not need such power, the "import" / "use" of the material (for example, in Java / C #) is much cleaner, and few people need the remaining cases;
  • Definition of constants. Why not just specify the const operator?
  • Macros. While C-style macros are very powerful (they can include statements such as returns), they also degrade readability. Generics / Templates are cleaner and, although less powerful, in several ways, they are easier to understand.
  • Conditional compilation is perhaps the most legitimate precedent for preprocessors, but once again it is painful for readability. Separating platform-specific code in platform source code and using common if statements is better for readability.

So, my answer is so far powerful, the preprocessor harms readability and / or is not the best way to deal with some problems. Newer languages ​​are generally very important for maintaining code, and for these reasons, the preprocessor looks outdated.

0
source share

This is your language so that you can create any features necessary for the language itself, without the need for a preprocessor. I do not think that a preprocessor should be needed, and it adds a layer of complexity and ambiguity on top of the language. Most modern languages ​​do not have preprocessors, and in C ++ you use it only when you have no other choice.

By the way, I believe that D handles conditional compilation without a preprocessor.

0
source share

It depends on what other features you offer. For example, if I have const int N, do you suggest me take N variables? There are N member variables, take an argument to build all of them? Create N functions? Perform N operations that do not necessarily work in loops (for example, pass N arguments)? N pattern arguments? Conditional Compilation? Constants that are not integers?

The C preprocessor is so absurdly strong in the right hands, you need to make a seriously powerful language so as not to justify it.

0
source share

I would say that although you should avoid the pre-processor for most of all that you usually do, it is still necessary.

For example, in C ++, a preprocessor is required to write a unit testing library such as Catch . They use it in two different ways: one for expanding statement 1 and one for nesting sections in test cases 2 .

But the preprocessor should not be abused to perform compile-time calculations in C ++, where const constructors and meta-programming expressions can be used.


Sorry, I don't have enough reputation to post more than two links, so I put this here:

  • github.com/philsquared/Catch/blob/master/docs/assertions.md
  • github.com/philsquared/Catch/blob/master/docs/test-cases-and-sections.md
0
source share

All Articles