Search code examples
haskellcompatibilitybackwards-compatibility

Can I use the cpp MIN_VERSION conditional for a haskell package?


I noticed that I can get backwards compatibility with base by adding a CPP conditional like so:

moo :: Moo
moo = Moo
    { happyMoo = Sound "moo"
#if MIN_VERSION_base(4,9,1)
    , upgradedMoo = Sound "moo"
#endif
    , sadMoo = Sound "moo"
    }

But I get an error when I specify package-level dependencies (ie: using something like #if MIN_VERSION_optparse-applicative(0,13,0)) and I'm having a hard time finding documentation on this GHC feature.

I'm wondering if something like MIN_VERSION_optparse-applicative exists and, if not, how do hackage maintainers keep code backward-compatible?


Solution

  • You can find the macro documented here and here.

    The issue here is package names get mangled in such macros; in particular, dashes get replaced by underscores. So it should be e.g.

    {-# LANGUAGE CPP #-}
    
    foo :: String
    foo = 
    #if MIN_VERSION_optparse_applicative(0,13,0)
      "x"
    #else
      "y"
    #endif