#ifdef considered harmful (was: DData)

Simon Marlow simonmar at microsoft.com
Thu Apr 15 12:13:34 EDT 2004


Robert Will writes:
> I think what Haskellers (and others) can do in most cases is 
> to factor out
> platform dependent parts to mini-modules which have different
> implementations but the same interface.  The version of such 
> a module is
> choosen by the path it resides in during the build process.  (Simplest
> case: the -i option of the compiler.)

I have to say, I dislike this approach to conditional compilation.

  - it puts the conditional in the build system rather than the source
    code, which means you have to look in the build system to find out
    what the code means.

  - it means if I want to ask questions like "hmm, how does this differ
    from the Windows version?"  I have to go find the windows version of
    the file.

  - the interface to the module is duplicated, and nothing is checking
    that all your implementations have the same interface.

OTOH, if you want to see a good example of #ifdefs gone wrong, take a
look at GHC's native code generator.  #ifdef can indeed be misused;
that's not a reason to throw it out altogether.

Cheers,
	Simon


More information about the Libraries mailing list