#ifdef considered harmful
Sven Panne
Sven.Panne at aedion.de
Wed Apr 7 11:54:00 EDT 2004
Alastair Reid wrote:
> What do people use macros for?
> 1) Aliases and abstraction: [...]
> 2) Defining symbols to be used in #ifs [...]
Nice summary, but I'd like to add two more points here:
3) Syntactic abstraction
One example you've already mentioned are the macros for Storable, another one
is from my OpenGL package:
http://cvs.haskell.org/cgi-bin/cvsweb.cgi/fptools/libraries/OpenGL/include/HsOpenGLExt.h?rev=1.5&content-type=text/x-cvsweb-markup
This macro allows me to define an OpenGL extension function in a very handy way:
EXTENSION_ENTRY("GL_EXT_fog_coord or OpenGL 1.4",glFogCoorddEXT,GLdouble -> IO ())
The actual implementation is hidden here, and I can simply use glFogCoorddEXT as
if it was a normal OpenGL function. (For OpenGL aficionados: Later I'd like to
implement per-context extension entries, and having the mechanics concentrated in
a single place is a *big* win.) It's not that I'm very proud of this tricky macro,
but Haskell simply doesn't allow this kind of abstraction.
4) Annoying FFI differences
Because WinDoze folks decided to have a different calling convention for DLLs
than the rest of the world, one has to use "stdcall" instead of "ccall" in
FFI imports from time to time. The easiest way to handle this is via a simple
macro CALLCONV:
foreign import CALLCONV "foo" foo :: IO ()
I really urge people proposing simple solutions for the CPP problem to try their
proposals on non-toy examples. "Keep it simple. But not too simple."
Cheers,
S.
More information about the Libraries
mailing list