Can a GHCI configuration file use CPP macros?

I thought it would be nice to configure my global GHCI configuration so that my commonly used import happens automatically when there are packages that provide them.

I tried to add this to ~/.ghc/ghci.conf:

:set -XCPP

#ifdef MIN_VERSION_containers
import           Data.Set (Set)
import qualified Data.Set as Set
import           Data.Map (Map)
import qualified Data.Map as Map
#endif

But apparently this does not work.

> stack repl
Configuring GHCi with the following packages: 
GHCi, version 8.0.2: http://www.haskell.org/ghc/  :? for help

<interactive>:24:1: error: parse error on input ‘#’

<interactive>:29:1: error: parse error on input ‘#’

Is there a way to get the CPP macros to work, or in some other way to accomplish what I'm trying to do?

+4
source share
1 answer

These macros cannot be used directly by GHCI, but they can be used in the file you use using the GHCI :addcommand .

, :

  • ~/.ghc/ghci.conf :add:

    > grep imports ~/.ghc/ghci.conf
    :add /home/chris/.ghc/imports.hs
    
  • ~/.ghc/imports/imports.hs CPP:

    > cat ~/.ghc/imports/imports.hs
    {-# LANGUAGE CPP #-}
    
    #ifdef MIN_VERSION_containers
    import           Data.Set (Set)
    import qualified Data.Set as Set
    import           Data.Map (Map)
    import qualified Data.Map as Map
    #endif
    
+4

Source: https://habr.com/ru/post/1670130/


All Articles