[Haskell-cafe] ifdef based on which OS you're on

Andrew Cowie andrew at operationaldynamics.com
Tue Feb 26 02:38:05 CET 2013


On Mon, 2013-02-25 at 00:28 -0500, Dan Cross wrote:
> 
> Why go to all that bother?  Why not just write code that writes out
> the path you want to take and link against it?
> 

{shrug} You could do that too. 

I spent a decade doing work in Java-land, a language where by design we
*didn't* have #ifdefs. Write once, run everywhere, they said. Yeah,
right. It was a royal pain in the ass, especially when writing language
bindings to native libraries. Portability is part of it, but including
coverage (or not) of features based on whether dependencies were present
was a biggie, too. So I'm a fan.

To your point about generating code and then having only one version at
compile time, that's a fair approach too. I would choose against it only
because I'd prefer (as in this example) the relevant lines be in the
source file where they belong, rather than having three definitions of
that function somewhere else (or worse, meta).

In the case I showed it's only one #define and one code block. But in a
case where you had to make decisions across a code base, then I think
#include "config.h" would be a workable choice.

Anyway, I thought it was cool, and as figuring it out took a bit of
doing I wanted to write it up.

AfC
Sydney






More information about the Haskell-Cafe mailing list