user2141650
user2141650

Reputation: 2857

CPP within Haddock

I can change haddocks based on a flag if I put the entire block within the CPP conditional branches:

#if SOME_MACRO
-- | Whether SOME_MACRO is true
-- >>> someMacro
-- True
someMacro :: Bool
someMacro = macroVal
#else
-- | Whether SOME_MACRO is true
-- >>> someMacro
-- False
someMacro :: Bool
someMacro = macroVal
#endif

And the documentation looks as you'd expect it to, and doctest also works as expected.

But that's maintenance nightmare for larger comments or code blocks. On the other hand, this does not seem to work:

-- | Whether SOME_MACRO is true
-- >>> someMacro
#if SOME_MACRO
-- True
#else
-- False
#endif
someMacro :: Bool
someMacro = macroVal

Why is that? Isn't CPP processed before Haddock? I have the intuition that it's because a newline is staying on after the CPP stage, but it's just an intuition

Upvotes: 3

Views: 108

Answers (1)

chi
chi

Reputation: 116139

I can't remember if, as you say, a newline is inserted or if -- worse -- some #line directive is also inserted, which breaks the Haddock block.

Anyway, I think one could workaround it as follows:

#if SOME_MACRO
#define SOME_MACRO_HK -- True
#else
#define SOME_MACRO_HK -- False
#endif


-- | Whether SOME_MACRO is true
-- >>> someMacro
SOME_MACRO_HK
someMacro :: Bool
someMacro = macroVal

Insert newlines in the macro as needed, possibly integrating the whole Haddock block in it. Not elegant by any stretch, but it should do.

Upvotes: 2

Related Questions