I'm contributing to Alex, and it obviously depends on a lot of libraries and should compile for a lot of versions.
I need to use a function that is only available from GHC 7.6.1 to handle an error better. So I want to use an #if ...
to import said function, else, I'll deal with the error differently.
I've seen some:
#if __GLASGOW_HASKELL__ >= 610
import Control.Exception ( bracketOnError )
#endif
So I did:
#if __GLASGOW_HASKELL__ >= 761
import Text.Read ( readMaybe )
#endif
Thinking that 761
is an alias to GHC version 7.6.1
, when I build the cabal package and try it out, the function doesn't get imported, even though I use The Glorious Glasgow Haskell Compilation System, version 7.8.4.
So after using a program to try it out, I find out that 7.8.1
identifies in __GLASGOW_HASKELL__
as 708
.
{-# LANGUAGE CPP #-}
module Main where
#if __GLASGOW_HASKELL__ == 708
ver = "==708"
#else
ver = "/=708"
#endif
main = putStrLn $ ver
And running it:
$ runhaskell if.hs
==708
How can I know what value should I use for 7.6.1
, or is there a better way to deal with this?
Read the fine documentation:
That's described in section 6.11.3.1 of GHC's users guide:
So for
7.6.1
, you would check__GLASGOW_HASKELL__ >= 706
. The reason for this are versions like7.10.x
.As Daniel Wagner indicated, the most correct way to check for package version is usually to use a Cabal
MIN_VERSION
macro. For example, you could useto determine if the
base
package is at least version 4.6.0, which is the earliest version with the function you seek.The
base
package is a little weird. It was used both by GHC and by the now-defunct Hugs and NHC implementations. Using the Cabal macro was then a more portable way to check thebase
version. These days, GHC is the only one usingbase
, so the portability argument is a little less clear, but that approach also has the advantage of checking the major and minor version numbers.Since
base
versions are very tightly tied to GHC versions, you can define a reasonable fall-back form ofMIN_VERSION_base
for compiling without Cabal, using__GLASGOW_HASKELL__
to estimate thebase
version. The currentcontainers
head conditionally defines such a fall-back.Update
As of GHC 8, the compiler itself has taken over the job of defining
MIN_VERSION
macros. This is great, because you now get to use these macros whether or not you build with Cabal. No more ugly approximations!