#ifdef considered harmful (was: DData)

Malcolm Wallace Malcolm.Wallace at cs.york.ac.uk
Tue Apr 6 17:12:40 EDT 2004


Graham Klyne <GK at ninebynine.org> writes:

> Please, no #ifdef's in standard library code!

Much as we all hate cpp, I'm afraid the standard libraries are
already heavily littered with #ifdefs.  Getting rid of them is
basically impossible.

> (a) where possible, use "run-time" tests rather than "compile-time" code 
> selection.  If the conditions are constant True or False, surely a compiler 
> should be able to eliminate the redundant code?

Almost never applicable.  #ifdefs are generally used to cater for system
differences, so more than likely the False branch simply won't compile.

> (c) another problem area is where type definitions (incl. function 
> interfaces)  differ between systems.  Again, eliminating spurious 
> differences would help.

There should be no externally visible type differences in standard
libraries.  But internal to the library, different systems might indeed
use different representations.  For an optimising compiler like GHC
this is really important, since certain extensions are a big help,
but simpler systems like Hugs and nhc98 still need a viable Haskell'98
representation if possible.

> (d) For those differences that cannot be eliminated implementing (in 
> Haskell) a portable preprocessor that can ship as part of every Haskell 
> environment?

I think this is ultimately going to be the most viable solution.
Let's write a simple 'hspp' (in Haskell?) that is backward compatible
with 'cpp -traditional' (although we might also design a newer nicer
syntax too), and distribute it with every compiler.  It can't be
too difficult, surely?  Half of the cpp parsing/selection code is
already available within hmake.  I would take on the project myself,
if I had time.

Regards,
    Malcolm


More information about the Libraries mailing list